Java 如何重新启动死线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18223931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a dead thread be restarted?
提问by Rakesh KR
What are all the different possibilities to bring the dead threadback to runnable state.
什么是所有的不同的可能性带来的死线回可运行状态。
采纳答案by Nargis
If you look at the Thread Life Cycle Image, there is no way you can go back to new position once your thread has terminated.
如果您查看线程生命周期图像,一旦您的线程终止,您将无法返回到新位置。
So there is no way to bring back the dead thread to runnable state,instead you should create a new Thread instance.
所以没有办法将死线程恢复到可运行状态,而是应该创建一个新的 Thread 实例。
回答by MadProgrammer
From the JavaDocs...
从JavaDocs...
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
多次启动一个线程是不合法的。特别是,线程一旦完成执行就可能不会重新启动。
You'll have to start a brand new instance.
您必须启动一个全新的实例。
Preferably, the actions you want to execute should be wrapped up in a Runnable
interface, that way you can simply pass the Runnable
to a new instance of Thread
最好,要执行应该在一个被包裹起来的操作Runnable
界面,这样你可以简单地传递Runnable
到一个新的实例Thread
回答by Ankur Lathi
When the execution of run() method is over, as the job it is meant is done, it is brought to dead state. It is done implicitly by JVM. In dead state, the thread object is garbage collected. It is the end of the life cycle of thread. Once a thread is removed, it cannot be restarted again (as the thread object does not exist).
当 run() 方法的执行结束时,它意味着完成的工作,它被带到死状态。它是由 JVM 隐式完成的。在死状态下,线程对象被垃圾收集。它是线程生命周期的结束。一旦一个线程被移除,它就不能再重新启动(因为线程对象不存在)。
Read more From Hereabout life cycle of Threads.
从这里阅读更多关于线程生命周期的信息。
回答by René Link
I guess you extended the Thread
class and you have overridden the run
method. If you do this you are tying the runnable code to the Thread
's lifecycle. Since a Thread
can not be restarted you have to create a new Thread
everytime. A better practice is to separate the code to run in a thread from a Thread
's lifecycle by using the Runnable
interface.
我猜你扩展了这个Thread
类并且你覆盖了这个run
方法。如果您这样做,您就是将可运行代码绑定到Thread
的生命周期。由于Thread
无法重新启动,因此您必须Thread
每次都创建一个新的。更好的做法是Thread
使用Runnable
接口将在线程中运行的代码与 a的生命周期分开。
Just extract the run
method in a class that implements Runnable
. Then you can easily restart it.
只需run
在实现Runnable
. 然后您可以轻松地重新启动它。
For example:
例如:
public class SomeRunnable implements Runnable {
public void run(){
... your code here
}
}
SomeRunnable someRunnable = new SomeRunnable();
Thread thread = new Thread(someRunnable);
thread.start();
thread.join(); // wait for run to end
// restart the runnable
thread = new Thread(someRunnable);
thread.start();
This practice makes it also easy if you need to remember the previous run state.
如果您需要记住之前的运行状态,这种做法也很容易。
public class SomeRunnable implements Runnable {
private int runs = 0;
public void run(){
runs++;
System.out.println("Run " + runs + " started");
}
}
PS: Use a java.util.concurrent.Executor
to execute Runnable
s. This will decouple thread management from execution.
PS:使用ajava.util.concurrent.Executor
来执行Runnable
s。这将使线程管理与执行分离。
Executor executor = Executors.newSingleThreadExecutor();
...
SomeRunnable someRunnable = new SomeRunnable();
executor.execute(someRunnable);
Take a look at Executor Interfaces
看看执行器接口
回答by Ashish Babu
The thread is a separate light weight process which executes independently irrespective of other threads. Once its execution is complete, there exists no means to restart it.
该线程是一个单独的轻量级进程,它独立执行而与其他线程无关。一旦它的执行完成,就没有办法重新启动它。
回答by Dhrumil Shah
Thread has many different state through out its life.
线程在其整个生命周期中有许多不同的状态。
1 Newborn State
1 新生儿状态
2 Runnable State
2 可运行状态
3 Running State
3 运行状态
4 Blocked State
4 阻塞状态
5 Dead State
5 死亡状态
Thread should be in any one state of above and it can be move from one state to another by different methods and ways.
线程应该处于上述任何一种状态,并且可以通过不同的方法和方式从一种状态移动到另一种状态。
When a thread is completed executing its run() method the life cycle of that particular thread is end.
当线程完成执行其 run() 方法时,该特定线程的生命周期结束。
We can kill thread by invoking stop() method for that particular thread and send it to be in Dead State.
我们可以通过为该特定线程调用 stop() 方法来杀死线程并将其发送到死状态。
回答by Martin James
The other obvious solution is: if you need the thread functionality many times, don't let the thread die. Instead of letting it exit, and so terminate itself, shove in a while(true) loop with a suitable wait at the top. You can then make it 'restart' its work by signaling it.
另一个明显的解决方案是:如果您多次需要线程功能,不要让线程死掉。与其让它退出,并因此终止自己,不如在顶部推入一个合适的等待时间的 while(true) 循环。然后,您可以通过向它发出信号来使其“重新启动”其工作。
This is much quicker, safer and more efficient than continually creating/terminating/destroying threads.
这比不断创建/终止/销毁线程更快、更安全、更有效。