Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question---start()

 
Huifen Lu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Henry, thanks for your reply, I got it.
 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Anindya Saha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic