Terminated Thread Revival

本秂侑毒 提交于 2019-12-02 01:02:46

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.

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.

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!