Thread类为什么要弃用stop resume suspend ?

1、stop

对于stop方法会立即停止某个运行线程的执行,不管正在执行的线程在执行什么正常操作。所以如果直接stop 可能会导致文件不会正常关闭,try catch 也不会再执行。 另外 stop 会立即释放运行线程它锁定的监视器对象。这样会导致其他线程提前得到了未完全“初始化”的数据,导致数据不一致。

This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
* ThreadDeath exception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
* other threads, potentially resulting in arbitrary behavior. Many
* uses of stop should be replaced by code that simply
* modifies some variable to indicate that the target thread should
* stop running. The target thread should check this variable
* regularly, and return from its run method in an orderly fashion
* if the variable indicates that it is to stop running. If the
* target thread waits for long periods (on a condition variable,
* for example), the interrupt method should be used to
* interrupt the wait.

2、Thread suspend

This method has been deprecated, as it is
* inherently deadlock-prone. If the target thread holds a lock on the
* monitor protecting a critical system resource when it is suspended, no
* thread can access this resource until the target thread is resumed. If
* the thread that would resume the target thread attempts to lock this
* monitor prior to calling resume, deadlock results. Such
* deadlocks typically manifest themselves as “frozen” processes.

上面的英文很好的解释了为什么不建议使用suspend 理由是 suspend 不会释放 线程所持有的的锁,那么,只能由resume 来唤醒其继续执行,那么当resume线程想要获取suspend线程所持有的的锁,然后在调用resume 那么就会发生死锁。
3、resume 存在的意义就是将suspend唤醒 当suspend不建议使用的时候 其也没有存在的意义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值