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

什么是happens-before 原则?

发布时间:2020-12-15 07:23:17 所属栏目:Java 来源:网络整理
导读:Java 中?happens-before 原则,是在 JSR-133 中提出的。 原文摘要: ? Each action in a thread happens-before every subsequent action in that thread. ? An unlock on a monitor happens-before every subsequent lock on that monitor. ? A write to a

Java 中?happens-before 原则,是在 JSR-133 中提出的。

原文摘要:

? Each action in a thread happens-before every subsequent action in that thread.
? An unlock on a monitor happens-before every subsequent lock on that monitor.
? A write to a volatile field happens-before every subsequent read of that volatile.
? A call to start() on a thread happens-before any actions in the started thread.
? All actions in a thread happen-before any other thread successfully returns from a join() on that thread.
? If an action a happens-before an action b,and b happens before an action c,then a happensbefore c.
? the completion of an object’s constructor happens-before the execution of its finalize method (in the formal sense of happens-before).

?

翻译过来加上自己的理解就是:

  • 程序次序规则:在一个线程内,按照程序代码顺序,书写在前面的操作先行发生于书写在后面的操作。(这里涉及到 CPU 指令重排,所以需要加入内存屏障保证有序性)
  • 管程锁定规则:对一个锁的解锁操作,先行发生于后续对这个锁的加锁操作。这里必须强调的是同一个锁。
  • volatile 变量规则:对一个 volatile 变量的写操作先行发生于后面对这个变量的读操作。
  • 线程启动规则:Thread 对象的 start() 方法先行发生于此线程的每一个动作。
  • 线程 join() 规则:被调用?join() 方法的线程的所有操作先行发生于 join() 的返回。
  • 传递性规则:操作 a 先发生于操作 b,操作 b 先发生于操作 c,则操作 a 先发生于操作 c。
  • 对象终结规则:一个对象的初始化完成(构造函数执行结束)先行发生于它的 finalize() 方法。


?


Java面试题汇总,总有一道卡住你!

(编辑:李大同)

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

    推荐文章
      热点阅读