aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread example" Watch "Thread example" New topic
Author

Thread example

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Hi,

Can anybody explain me the working of line4?? Want to know how super(runnable) reacts?

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Line 4 is calling the Thread constructor that takes a Runnable parameter. The Javadoc for this constructor shows it creates a Thread object that will delegate to the parameter's class when start/run are called.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Thanks. Got it.

But I still have one confusion. why we use super() here? As it is used to call superclass method, and is that is the case superclass, in this case "Thread" , its run() should be called which will do nothing.

Am I right?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

The superclass' run method delegates to your Runnable's run method.

Sequence:
new Extender(new Implementer()) --> calls superclass which stores a reference to Implementer.
.start() --> calls run method on the new thread which delegates to Implementer's run method.
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Thanks Jeanne.
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Jeanne Boyarsky wrote:The superclass' run method delegates to your Runnable's run method.

Sequence:
new Extender(new Implementer()) --> calls superclass which stores a reference to Implementer.
.start() --> calls run method on the new thread which delegates to Implementer's run method.



I thought the code highlighted in red will call Implementers run method as you stated,,, but surprisingly!!! the above code is calling Extenders run method, not Implementers

Then, I dig into thread class and found the catch in run method implementation of Thread class



So,the conclusion is when we extend a Thread and override run method,then run method of target/Runnable is ignored

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

I stand corrected. The following outputs:
runnable
runnable
extended

Which does show that Thread only delegates to Runnable if you leave run out.


Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

But as a cool note, this calls the delegated Runnable via super.run() in addition to the current thread. So it makes sense - if you override run and mask the default behavior, it no longer delegates to the runnable. If you keep the superclass' run via calling it from your run, the logical behavior is still there.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread example