• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 41381
853
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: 41381
853
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: 41381
853
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: 41381
853
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 lasts forever. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic