• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Thread Question---start()

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


A. Prints : MyThread: start() followed by MyRunnable:run()
B. Prints : MyThread: run() followed by MyRunnable:start()
C. Prints : MyThread: start() followed by MyRunnable:start()
D. Prints : MyThread: run() followed by MyRunnable:run()
E. Compile time error
F. None of the above

Can someone please explain in detail why the answer is A instead of D?
Thanks a lot in advance!

Huifen
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, the MyThread class overrides the start() method. When you call the start() method, it prints the start message. It doesn't actually start a thread that calls the run() method -- that code has been overridden.

Henry
 
Huifen Lu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, thanks for your reply, I got it.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry, can u please explain it in detail, i did not get the explaination.., i think choice sd be A.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by cs singh:
Hi Henry, can u please explain it in detail, i did not get the explaination.., i think choice sd be A.



Not sure what are you asking... You agree with me that it is choice A, but you don't understand why I think it is choice A?

BTW, why do you think it is choice A?

Henry
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be A
MyThread class overrides the start() method. So when myThread.start() is called it calls the overridden start() method in MyThread class and not the start() method of Thread class beacsue the reference variable type is MyThread. Hence it will print
MyThread: start()
MyRunnable class does not override start() method. Its just any other method. Now Thread thread = new Thread(myRunnable); creates a new thread from myRunnable. Calling thread.start(); will look for Thread class' start method beacsue the reference variable type is Thread which would eventually try to call MyRunnable's run method.Hence it will print
MyRunnable: run()
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic