7

If I use start() on a Thread object and the run() method returns, is it possible to call start() again?

eg,

MyThread myThread = new MyThread();
myThread.start();
// run method executes and returns in 2 seconds
// sleep for 5 seconds to make sure the thread has died
myThread.start();

I'm just wondering because my code is throwing IllegalThreadStateExceptions, so want to know if it's because you can't do the above.

Matt
  • 11,157
  • 26
  • 81
  • 110
  • @Stephen I did look at the javadoc, but only for the start() method which didn't make it sound very clear. – Matt Mar 22 '11 at 22:17
  • Huh? What is unclear about the sentence *"It is never legal to start a thread more than once"* on a method called `start()`? – Stephen C Mar 22 '11 at 22:49
  • I was reading this: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#start%28%29 – Matt Mar 22 '11 at 23:31
  • Curious: what are you trying to do? I suspect that using a scheduled executor (`ScheduledExecutorService`) is closest to what you want to achieve, but please feel free to fill me in. – C. K. Young Mar 24 '11 at 06:50

5 Answers5

12

No, you can't. And the Javadoc for the Thread.start() method tells you that!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
dty
  • 18,795
  • 6
  • 56
  • 82
  • Oops.. so does "started" mean "has been started at some point", rather than "not dead"? Is there anything else I could do to re-start a thread? – Matt Mar 22 '11 at 22:13
  • Where does 'started' appear? The documentation I'm looking at says "It is never legal to start a thread more than once". If you have written a `Runnable` instead of extending `Thread` then the executor framework gives you the ability to re-submit a job, or have it run periodically, etc. – dty Mar 22 '11 at 22:15
  • http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#start%28%29 "Throws: IllegalThreadStateException - if the thread was already started." But thanks, at least I know now. – Matt Mar 22 '11 at 22:20
  • The javadoc means "It is never legal to call `start()` twice" – matt b Mar 22 '11 at 22:20
  • Ah yeah. Then as @GregInYEG said. It's a bit like the `isConnected()` method on `Socket` - it just tells you that the socket has, at some point, been connected. Utterly useless! – dty Mar 22 '11 at 22:21
4

From a comment:

Is there anything else I could do to re-start a thread?

You could use ThreadPoolExecutor, which would allow you to pass in tasks and let the service assign a thread to a task. When the task is finished, the thread goes idle until it gets the next task.

So, you don't restart a thread, but you would redo/resume a task.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Nope.

From the Javadoc for java.lang.Thread:

It is never legal to start a thread more than once.

Peter C
  • 6,219
  • 1
  • 25
  • 37
0

From the javadoc:

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

See the Thread.start() javadoc for more information.

There are other ways to accomplish what you are trying to do. For example, you could use new Threads that continue the work that was done in the Thread that has finished execution. You may also want to investigate the java.util.concurrent package.

Greg
  • 33,450
  • 15
  • 93
  • 100
0

Perhaps there is a better way of doing this if you want the thread to stop and restart multiple times. I have a tile caching thread in C++ that does something similar; it pauses when it's finished, and unpaused when it's needed again. I am new to Java, but from what I can tell, you can use Object.wait() to pause, and Object.notify() to resume threads. Maybe you could check those out in the documentation and redesign your thread to pause and resume instead of exiting.

TurtleToes
  • 2,047
  • 2
  • 17
  • 17
  • Don't know why you would downvote it... it works in C++... starting a new thread every time you need the same thread is a waste... if you need the same thread to retain itself, it's better to simply pause and resume it. – TurtleToes Mar 22 '11 at 23:34
  • 1
    Using `wait` and `notify` is _totally_ the wrong way to go about parking a thread for use in executing multiple tasks. The right way, BTW, is to use an executor (such as `ThreadPoolExecutor`) to do the thread parking for you. – C. K. Young Mar 24 '11 at 06:48