Java 线程何时达到“死亡”状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1269931/
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
When does a Java Thread reach the 'Die' State
提问by Johanna
In Java, Die is one of the states on a thread.
在 Java 中,Die 是线程上的状态之一。
What causes a thread to enter this state?
是什么导致线程进入这种状态?
回答by idrosid
From the Thread API, here is a complete list:
来自Thread API的完整列表如下:
- If the run() method returns.
- If an exception is thrown that propagates beyond the run method.
- If it is a daemon thread and all non-daemon threads have 'died'
- If the exit method of class Runtime has been called (even at another thread).
- 如果 run() 方法返回。
- 如果抛出的异常传播到 run 方法之外。
- 如果它是一个守护线程并且所有非守护线程都“死了”
- 如果已经调用了 Runtime 类的退出方法(即使在另一个线程中)。
回答by Rob
All Thread
s die either by returning from the call to the run
method or by throwing an exception that propagates beyond the run
method.
所有Thread
s 要么通过从run
方法调用返回,要么通过抛出传播到run
方法之外的异常而死亡。
回答by Spencer Ruport
Threads die in the following situations:
线程在以下情况下死亡:
- When the method it runs finishes (or throws)
- When the process is terminated
- When the computer is turned off or reset.
- 当它运行的方法完成(或抛出)
- 当进程终止时
- 当计算机关闭或重置时。
回答by Cesar Hermosillo
There are two ways for a thread to die:
线程死亡有两种方式:
a) It could die of natural causes which is when the run() method finishes or return,
a) 它可能因自然原因而死亡,即 run() 方法完成或返回时,
or
或者
b) it could be kill by using the stop() method or when something goes wrong with the program(This could be an Exception) or computer.
b) 它可以通过使用 stop() 方法或当程序出现问题(这可能是一个异常)或计算机时被杀死。