Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question---start() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question---start()" Watch "Thread Question---start()" New topic
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: 18534
    
  40

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: 18534
    
  40

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()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question---start()
 
Similar Threads
Thread
Threads Behavior of run and Start methods.
Overriding the start method of Thread Class
Whizlabs Thread Question
Thread Doubt