Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between start() and run() in thread

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic