| Author |
Thread Question---start()
|
Huifen Lu
Greenhorn
Joined: Feb 27, 2006
Posts: 7
|
|
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
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Huifen Lu
Greenhorn
Joined: Feb 27, 2006
Posts: 7
|
|
Henry, thanks for your reply, I got it.
|
 |
cs singh
Ranch Hand
Joined: Dec 28, 2005
Posts: 36
|
|
|
Hi Henry, can u please explain it in detail, i did not get the explaination.., i think choice sd be A.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
 |
Anindya Saha
Greenhorn
Joined: Mar 12, 2006
Posts: 6
|
|
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()
|
 |
 |
|
|
subject: Thread Question---start()
|
|
|