aspose file tools*
The moose likes Threads and Synchronization and the fly likes By any chance can we start a thread twice ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "By any chance can we start a thread twice ?" Watch "By any chance can we start a thread twice ?" New topic
Author

By any chance can we start a thread twice ?

Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52


From the Java Specs :

java.lang.Thread

public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.


Does the text in red above means if the thread is still in runnable state (and not yet moved to running), start() can be called on it without the exception (IllegalThreadState) being thrown ?

Is there any programmatic way to ensure that a thread remains in Runnable state for some specified time and doesnt move to running ?

Thanks


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Abhineet Kapil wrote:Does the text in red above means if the thread is still in runnable state (and not yet moved to running), start() can be called on it without the exception (IllegalThreadState) being thrown ?


No, because that would directly contradict the sentence immediately before it.

Is there any programmatic way to ensure that a thread remains in Runnable state for some specified time and doesnt move to running ?


No. (If you don't want a Thread to start running, then don't call its start() method.)
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Thanks Paul


So, in short, whenever thread is started more than once we cant escape java.lang.IllegalThreadStateException
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

As Paul said, no.

But there really should be no reason to; simply create a new Thread and pass it the Runnable object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: By any chance can we start a thread twice ?