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 ]
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.
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.
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 "; } }
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 by:autobot
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, that will fit in a stocking