Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread example

 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne.
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic