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

Threads question from Dan's Topic Exam 4

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

Dan or anyone, I cannot understand why the program will print A when the Runnable target is of type B ?
TIA
Sri
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Thread.run() method is responsible for executing the run() method of the Runnable that was passed to the Thread constructor.
However, class A has overridden the Thread.run() method, so the Thread.start() method calls A.run(). "B" will only be printed if the overriding run() method calls super.run():

[ February 08, 2003: Message edited by: John Paverd ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Thanks for your reply
I am still confused
in class C, as we are starting thread using an instance of A, the thread method run of A is called? Is it ?
Sri
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:
in class C, as we are starting thread using an instance of A, the thread method run of A is called? Is it ?


Short answer: Yes.
Long answer:
new A(new B()).start(); creates a new instance of A, passing a new instance of B to the constructor. Then the start() method of A is called. Since A did not override the start method, the start method that A inherited from Thread is called. The start method in Thread creates a new thread, and causes the new thread to execute the run method. Since A overrode the run method in Thread, A's run method is executed. That's why the output of the code you posted is A.
If you run the code that I posted, the output is AB, because of the super.run() statement that I added to A.run()
Hope that clarifies things for you.
[ February 08, 2003: Message edited by: John Paverd ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Thanks,
Yes it answered my question. The world looks less troublesome
Sri
 
reply
    Bookmark Topic Watch Topic
  • New Topic