2

I am storing a bunch of threads objects in an arraylist. I want to be able to start these threads at random. Same thread can be started more than once. Before I start a thread object, I check on whether the thread is alive, and if they have either of NEW or TERMINATED status. This restriction because, I don't want to disturb the 'busy' threads. Now, for NEW threads, this works fine. But for TERMINATED thread, I get an exception.

When a thread ends, shouldn't it go back to being 'new'? Or are threads 'disposable' - like use once and done?

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
moejoe
  • 145
  • 2
  • 11
  • possible duplicate of [Can I start a thread again after it has died?](http://stackoverflow.com/questions/5398459/can-i-start-a-thread-again-after-it-has-died) – dty Mar 23 '11 at 21:11
  • @Hank It is true - thanks for the link. – moejoe Mar 23 '11 at 23:14

5 Answers5

2

As it says in the documentation for Thread.start(), "It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution."

It is better for you to keep hold of Runnable instances and implement your own logic for keeping track of when the execution of each one of them finishes. Using an Executor is probably the simplest way to run the Runnables.

Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
1

This is the way I did it

class GarbageDisposalThread extends Thread {
public void start() {
   try {
      super.start();
   } catch( IllegalThreadStateException e ) {
      this.arrayList.remove(this);
      this.arrayList.add( new GarbageDisposalThread( this.arrayList ));
   }
}
private GarbageDisposalThread() {
}
public GarbageDisposalThread( ArrayList<Whatever> arrayList ) {
   this.arrayList = arrayList;
   this.start();
}
public void run() {
   // whatever the code
}
private ArrayList<Whatever> arrayList = null;
}

that's it! you can change the code according to your needs :P

Aurangzeb
  • 11
  • 1
  • this is actually creating a new thread in-place of the old one... well not in-place exactly (but you can modify the code little-bit to do that!) – Aurangzeb Jun 07 '12 at 15:04
1

You should probably be using the awesome stuff provided in java.util.concurrent. Based on your description, ThreadPoolExecutor sounds like a good thing to check out.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
0

Java threads cannot be restarted.

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

From another post...

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.

moejoe
  • 145
  • 2
  • 11