Forums Register Login

Difference between start() and run() in thread

+Pie Number of slices to send: Send
hello ranchers...,

As we know that start() is used to call run() implicitly ,it is also possible that we call directly run() then "what is the difference when we call run() directly on a Thread object and when we call start() on a Thread object" .

thanks in advance
[ March 05, 2006: Message edited by: faisal usmani ]
+Pie Number of slices to send: Send
When your original thread calls the start() method, a new thread represented by the thread object is started. This new thread will then call the run() method, while your original calling thread has returned, and running something else.

When your original thread calls the run() method, it acts just like any other method call. No new thread is started. It doesn't return, and run something else, until it completes the execution of the run() method.

Henry
+Pie Number of slices to send: Send
Another way to look at this is that when you extend Thread you write run() so you know exactly what it does. When you call it, it does nothing more or less than what you wrote which means no thread-related stuff. start() was written in the library, and hides a lot of fairly serious magic to create and start a new thread.
+Pie Number of slices to send: Send
Okay so here is my code. I keep getting a "Illegal state monitor exception"
I looked in the Java API for this exception it sais that it is thrown if
the waiting object doesn not actually own the monitor of the object being
waited on.... What do they mean by "monitor" ?

<code/>
final SwingWorker watcher = new SwingWorker()
{
//replace with if(a.isReady())
final SwingWorker worker = new SwingWorker()
{
public Object construct()
{ try
{
doActionThatTakesWhile()
}
catch(Exception e)
{ e.printStackTrace(); return e.getMessage();
}
}
}

public Object construct()
{
worker.start();
while(worker.getValue() == null)
{
try{

this.wait(1000); //exception is thrown here System.out.println("still working");
}
catch(Exception e)
{
e.printStackTrace();
}
}

return "finished doing action that takes a while ";
}
}

watcher.start();

}
+Pie Number of slices to send: Send
oops --- I meant to post that in a similar, but different forum.

I think its relevant to this discussion, however...

any solution ideas would really be appreciated !
+Pie Number of slices to send: Send
This forum is fine for the question. A new topic would have been a good idea, though. Continuing an old one might not get the attention you want. And Real Soon Now somebody is going to give you a strong hint to change your name to something more believable.

Anyhow, add a synchronized block around the object you wait on. It's a little risky to synchronize on "this" so another object that your two threads could share privately would be good.
[ March 06, 2006: Message edited by: Stan James ]
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1214 times.
Similar Threads
start and run method
Calling Thread's run V/s Runnable's run
start() method of the Thread class
threads
invoking run() method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:27:19.