• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between start() and run() in thread

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic