0

I have one single thread created, and a method will call this thread to .start(), this method might be called again before the thread finishes its work.

i added a return; inside the run(), so it will die when it finishes its work and be started again. But it gives me a IllegalThreadStateException when i try to start it after its work is done. Is the thread not shutting down? I thought adding a return; would shut it down.

PhoonOne
  • 2,678
  • 12
  • 48
  • 76
  • possible duplicate of [How to start/stop/restart a thread in Java?](http://stackoverflow.com/questions/1881714/how-to-start-stop-restart-a-thread-in-java) – Ordous Dec 03 '14 at 19:55
  • 1
    A `Thread` _object_ is not the same thing as a thread. The actual thread is not created until you call `.start()`. It does what it does, and then it ceases to exist after the `.run()` method either returns or throws an exception. The designers of the `Thread` class wanted to make sure that each `Thread` corresponds to one, and only one thread. Since a thread only runs one time and then ceases to exist, it makes sense that you are only allowed to call `t.start()` one time for any given `Thread` object, `t`. – Solomon Slow Dec 03 '14 at 20:00
  • @jameslarge - why leave an answer as a comment? – Elist Dec 03 '14 at 20:04

3 Answers3

0

From the Javadoc:

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.

Throws: IllegalThreadStateException - if the thread was already started.

Look closely at the last 2 sentences, and you shall find your answer.

Ordous
  • 3,844
  • 15
  • 25
0
When you wish to stop the thread, you set this flag and call join() on the thread and wait for it to finish.

From How to properly stop the Thread in Java?

Thread objects are only meant to be started once. If you need to stop/interrupt a Thread, and then want to start it again, you should create a new instance, and call start() on it:

thread.interrupt();  // if you need to make sure thread's run() method stops ASAP
thread = new MyThreadSubclass();
thread.start();

From Stop thread and again start giving IllegalThreadStateException in blackberry

Community
  • 1
  • 1
Jase Pellerin
  • 397
  • 1
  • 2
  • 13
0

You can't call start on the same Thread object more than once. If you have some work that needs to be done multiple times on a separate thread every now and then, but must also store state between runs, you could then implement a Runnable instead of Thread (assuming that's what you are doing). After that you could pass the same Runnable instance to a new Thread object whenever you need to execute the work again and just call start() on that new Thread object.

NESPowerGlove
  • 5,496
  • 17
  • 28
  • An application that creates a new thread _every time_ it needs to perform a background task, is an application that is crying out for a thread pool (e.g., `Executors.newFixedThreadPool()`). Creating and destroying threads is expensive, and it's risky too if the application has no cap on how many background threads it can kick off a the same time. – Solomon Slow Dec 03 '14 at 20:04
  • I'd agree. But my suggestion here is what could work with minimal change (few lines) to get to something that at least works. Refactoring to better multithreaded constructs is the best approach in the long run. – NESPowerGlove Dec 03 '14 at 20:07