wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Difference between start() and run() in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between start() and run() in thread" Watch "Difference between start() and run() in thread" New topic
Author

Difference between start() and run() in thread

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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 ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
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();

}
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
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 !
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between start() and run() in thread