zoukankan      html  css  js  c++  java
  • 什么是 happens-before 原则?

    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() 方法。


      

      

    来一道刷了进BAT的面试题?

  • 相关阅读:
    [模板] 主席树
    [模板] 替罪羊树
    [模板] Treap
    [LUOGU] P4342 [IOI1998]Polygon
    [JOYOI] 1051 选课
    poj 1845 数论(唯一分解定理+分治法求等比数列前n项的和mod m的值)
    poj 2418 bst统计字符串
    hdu 3791 二叉排序树
    hdu 3999 二叉排序树
    toj 3711 水题
  • 原文地址:https://www.cnblogs.com/ConstXiong/p/11688034.html
Copyright © 2011-2022 走看看