wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confuon in Thread Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confuon in Thread Problem" Watch "Confuon in Thread Problem" New topic
Author

Confuon in Thread Problem

Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125



Hi Ranchers,

I understand the process like ::
1.Create the Runnable Object ……
2.Create a Thread instance & give it your target Runnable created.
3.Now when we invoke start() on Thread instance it’ll invoke run method of target runnable.

But in above example as we created a Thread instance at (a),(b) & given it a Runnable created.When we invoked start method on different Threads created , (a) & (b) are calling
Their own (Thread subclass) methods instead of Target Runnble’s run() method (irrespective of line 1 or 2 is commented at a time).

Why they are calling thir own class’ run() method ???
Whereas if we use statement

Thread ob = new Thead(rd);
Ob.start();

Will call the run() method of target Runnable .In this case why not Thread class’ run() method called(which does nothing) ???

I am in big confusion please help me.

Thanks & Regards


(attempted formatting code)
[ October 03, 2006: Message edited by: Barry Gaunt ]

SCJP 1.4, SCWCD 1.5
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Thread API.

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Thread's run method doesn't do nothing. It calls run on the runnable it was passed at contsruction. If not passed a runnable it does nothing.

By overriding run() in MyThread1 and MyThread you lose this functionality.
Karthigeyan Kannabiran
Greenhorn

Joined: Sep 08, 2006
Posts: 5
Hi,
This is actually method overriding. Actually in Thread class the implementation given like
public void run() {
if (target != null) {
target.run(); // target is the Runnable object which we are passing
}
}

that is why it calls the super class(Runnable's )run method. Since you are overridden the method it doesnot calls the Runnbale's run method.. and did what you said ..

Please correct me if i am wrong..




Thanks & Regards<br />Karthik
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Originally posted by Karthigeyan Kannabiran:
Hi,
This is actually method overriding. Actually in Thread class the implementation given like

that is why it calls the super class(Runnable's )run method. Since you are overridden the method it doesnot calls the Runnbale's run method.. and did what you said ..

Please correct me if i am wrong..


Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Originally posted by Keith Lynn:
This is from the Thread API.

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.


First of all thanks for your quick response.i do completely understang what you said but in this case I passed a Runnable to Thread subclass and its constructor passes it to Thread class
.ultimately what Thread class gets the target Runnable whose run() method
should be called.but why Thread sublass method is called and why not Runnable…..

Still confused ………. Please help
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226


In your code above you subclass Thread and override the run method.



In the above code you make an instance of MyThread1 (with confusing names I might add, the instance of MyThread is named myThread1) then you call start(). After start() has done the work of creating a new thread it invokes run(). You have overridden run() so Thread.run() doesn't get called MyThread1.run() gets called instead. Thread.run() has the code to call run()on a runnable, if passed. Since this code is overridden your runnable run() method is never called.

Usually you subclass thread or implement runnable - not both at the same time.

Hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confuon in Thread Problem
 
Similar Threads
Reg. Some Thread basics
Thread target
threads!
why run() method in Runnable interface is not called ?
begin execution of a thread -- start or run?