• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Q on Threads

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:


What is the result?
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.

Answer: B
Can some one please clarify,
Runnable is the job and Thread is the worker(a line directly quoted from K&B).
So in the above example, i have given a job(runnable) to thread(worker), then doesn't the thread execute runnable's run method?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread implements Runnable, so at runtime run() in Thread is executed.
correct me if I m wrong
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ahmed Yehia:
Thread implements Runnable, so at runtime run() in Thread is executed...


That's true, but if the Thread was constructed using a separate Runnable, then the implementation of Thread's run method is to invoke the Runnable's run method.
[ September 25, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If "t" were just an instance of Thread, then its own run method would invoke the Runnable's run method, and you would get the behavior you expected. However, in this example, "t" is a subclass of Thread, and its run method has been overridden so that it does not invoke the Runnable's run method.

If you still want the overridden run method to invoke the Runnable's run method, then you could call super.run() to get the superclass behavior...
 
I found some pretty shells, some sea glass and this lovely tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic