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

Thread

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt from Dan's question set.

The Output is:
The above code prints A
My question is:
When we pass the Runnable target as B then why does it invoke the run() method of A. Although we invoke A's constructor only.
Thanks
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call the .start method of a Thread class, according to the JavaDoc:
[calling the start method] causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread
So, the run method will be called. Since A's start method is used and A's run method prints A, A is printed. The runnable passed to A's constructor has no effect.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pang
Please could the execution of the code, be explained more clearly.I am still confused.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe you could look at the Thread.java.
You will see that the run method there looks like this :

And when start of the thread is invoked it will do the run() method. And you overridden the run() method in the original thread. So it won't execute the 'target' but only your run method.
Look in the source of java wich comes with every SDK.
I hope this helps
greeting Jos ( who is has to take his exam april the 6th )
[ April 02, 2004: Message edited by: j zaal ]
 
Politics n. Poly "many" + ticks "blood sucking insects". 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