加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 调用Thread.stop方法时可能会发生什么样的损坏?

发布时间:2020-12-15 04:18:59 所属栏目:Java 来源:网络整理
导读:参见英文答案 Thread.stop() – deprecated????????????????????????????????????4个 我刚刚阅读了Thread.stop函数的JDK文档.它说, Stopping a thread causes it to unlock all the monitors that it has locked. If any of the objects previously protecte
参见英文答案 > Thread.stop() – deprecated????????????????????????????????????4个
我刚刚阅读了Thread.stop函数的JDK文档.它说,

Stopping a thread causes it to unlock all the monitors that it has locked. If any of the objects previously protected by these monitors were in an inconsistent state,other threads may now view these objects in an inconsistent state.Such objects are said to be damaged. When threads operate on damaged objects,arbitrary behavior can result.

我只是不太明白.不一致状态意味着什么?

任意行为可以是什么?有人可以向我解释一下吗?

(我的问题与您认为重复的问题略有不同.我强调可能的损坏,而前者强调可以用什么方法代替Thread.stop.)

解决方法

考虑任何类型的复杂数据结构,比如双链表.线程锁定结构,查看并更改它,然后释放锁.所以考虑:

>锁定列表.
>调整前进指针.
>调整相应的反向指针.
>解锁.

想象一下,如果一个线程已经完成了步骤1和2,那么你就停止了它.双向链表无效,因为对它的修改已完成一半.如果另一个线程获得此锁并尝试遍历列表,则会导致灾难.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读