为什么Thread.stop()如此危险 [英] Why is Thread.stop() so dangerous

查看:126
本文介绍了为什么Thread.stop()如此危险的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Thread.stop()这么危险?

为什么建议使用 Thread.interrupted()而不是?

Why is it advisable to use Thread.interrupted() instead?

我知道 stop 已弃用。还有什么让它不安全?

I know stop is deprecated. What other thing makes it unsafe?

我有没有可以使用停止方法的地方?如果是这样给我一个例子。

Is there any place where I can use stop method? If so give me an example.

推荐答案


为什么Thread.stop()如此危险?

Why is Thread.stop() so dangerous?

这里详细描述了这些问题: http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

The problems are described in detail here: http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html


为什么建议使用Thread.interrupted()代替?

Why is it advisable to use Thread.interrupted() instead?

因为 Thread.interrupt()让中断的目标线程在它知道 safe 时达到响应这样做。

Because Thread.interrupt() lets the target thread of the interrupt respond when it reaches a point when it knows it is safe to do so.


我知道不推荐使用stop。还有什么让它不安全?

I know stop is deprecated. What other thing makes it unsafe?

见上面的链接。


有什么地方可以使用stop方法吗?如果是这样给我一个例子。

Is there any place where I can use stop method? If so give me an example.

理论上,如果你知道你正在停止的线程:

In theory, if you knew that the thread you were stopping:


  • 没有更新与其他线程共享的数据结构,

  • 没有使用wait / notify或更高级别的同步类,或者依赖于它们的类,

  • 以及其他一些事情。

例如,我认为它会是安全到 stop()此主题:

For example, I think it would be safe to stop() this thread:

new Thread(new Runnable(){
    public void run(){for (long l = 0; l > 0; l++){}}).start();

然而,在大多数情况下,进行分析是非常困难的弄清楚如果调用 stop()真的是安全的。例如,您必须分析线程使用的每个可能的代码(包括核心和第三方库)。因此,默认情况下会使其不安全。

However, in most cases it is too difficult to do the analysis to figure out if calling stop() will really be safe. For instance, you have to analyse every possible bit of code (including core and 3rd-party libraries) that the thread uses. So that makes it unsafe by default.

这篇关于为什么Thread.stop()如此危险的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆