aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question" Watch "Thread question" New topic
Author

Thread question

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148





The above code is from Khalid Mughal . At line 1 the run method of Extender is executed. According to me the run of Class A1 should run because the constructor of Extender with argument invokes the super(runnable) so the run of runnable should run .

the output is Extender Extender Implementer
but i think the output should be Implementer Extender Implementer

can anyone explain why the run method of A1 is not invoked at line 1?


SCJP 6.0 (86%) SCWCD 5 (94%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Actually this happens because the start method of Thread class calls the run method of the Thread class (or any subclasses like your Extender class - polymorphism). It is the responsibility of the run method to call the run method of the runnable object. The Thread class is implemented something like this

So for line 1 to work like line 3, the Extender class will have to be modified like this


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
thanks ankit
Weiheng Zhang
Greenhorn

Joined: Jan 23, 2010
Posts: 9

instance of Extender gets |Extender|
instance of A1 gets |Implementer|
That's correct, but the point is that there are 3 threads, the order is unpredictable for it's controlled by JVM rather than the order of source code, if run again, you may see different results.


- SCJP SCWCD
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Ankit Garg wrote:Actually this happens because the start method of Thread class calls the run method of the Thread class (or any subclasses like your Extender class - polymorphism). It is the responsibility of the run method to call the run method of the runnable object. The Thread class is implemented something like this

So for line 1 to work like line 3, the Extender class will have to be modified like this

Hi Ankit, I tried doing what you are saying. but still it prints the same. It does not run the method from Implementer.here is the code,
can you also explain when which method runs?I am very confused.

when I pass different objects to the Thread constructor, it executes the run method of that object meaning new Thread(new MyThread()).start(); calls executes run method of MyThread class.
Can any one please explain, what is the rule ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question
 
Similar Threads
Dan's thread exam - question 23
Threads Doubt
another of Dan's question
Mughal SCJP Thread question - the use of super.start()
Thread objects and run() method