• 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

By any chance can we start a thread twice ?

 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul


So, in short, whenever thread is started more than once we cant escape java.lang.IllegalThreadStateException
 
Ranch Hand
Posts: 43
Mac Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic