• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread example

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne.
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic