Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

By any chance can we start a thread twice ?

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul


So, in short, whenever thread is started more than once we cant escape java.lang.IllegalThreadStateException
 
Rishi Shah
Ranch Hand
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul said, no.

But there really should be no reason to; simply create a new Thread and pass it the Runnable object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic