为什么Thread.stop()
如此危险?
为什么建议使用Thread.interrupted()
?
我知道stop
已被弃用。还有什么东西让它不安全?
我可以使用stop
方法吗?如果是这样给我一个例子。
答案 0 :(得分:9)
为什么Thread.stop()如此危险?
这里详细描述了这些问题:http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
为什么建议使用Thread.interrupted()代替?
因为Thread.interrupt()
允许中断的目标线程在它知道安全时到达某个点时响应。
我知道停止已被弃用。还有什么东西让它不安全?
见上面的链接。
有什么地方可以使用停止方法吗?如果是这样给我一个例子。
理论上,如果你知道你正在停止的线程:
例如,我认为stop()
这个帖子是安全的:
new Thread(new Runnable(){
public void run(){for (long l = 0; l > 0; l++){}}).start();
但是,在大多数情况下,进行分析以确定调用stop()
是否真的安全是太难。例如,您必须分析线程使用的每个可能的代码(包括核心和第三方库)。因此,默认情况下会使其不安全。