*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Doubt " Watch "Thread Doubt " New topic
Author

Thread Doubt

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
O/p of below porogram is
MyThread: start()
MyRunnable: run()
Can any one explain why o/p is not
MyThread: start()
MyThread: run()
MyRunnable: start()
MyRunnable: run()





SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
Hi divya

I will try my best to answer.

mythread.start()
The above statement is not starting the new thread.It just calls the overridden start() method in MyThread class.

Thats why you got the output : MyThread: start()

thread.start();
This statement starts the newly created thread.This will access start() method declared in Thread class which in turn calls run() method declared in MyRunnable class which results in the output MyRunnable: run()

mythread.start() calls the start() of MyThread alone
thread.start() calls the start() of Thread class which calls run() of MyRunnable.

Thanks
Praveen SP
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
I got the output. But I have another question. Why MyRunnable class is able to implement start() method. It is not a Runnable interface method.
can you explain?


SCJP-1.5<br />SCWCD-1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Originally posted by dolly shah:
I got the output. But I have another question. Why MyRunnable class is able to implement start() method. It is not a Runnable interface method.
can you explain?


For the same reason the MyRunnable class is able to implement a method named join(), alive(), hello(), goodbye(), mymethod1(), mymethod2(), etc. Just because you implement an interface, doesn't mean that you are not allowed to have methods that is not part of that interface.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Even Object class doesn't have start() method. From where MyRunnable can implement this method?
Michael Sanchez
Greenhorn

Joined: Apr 30, 2006
Posts: 1
A class may define new methods - there is no requirement that methods defined in a class be first declared in an interface.

However, *if* a class *does* implement an interface, then it must implement all of the methods declared in that interface (or be declared abstract itself).
 
 
subject: Thread Doubt