完美解释 Java 中的 内存模型 JMM 规则

Happens-before的原则

英文:

  • Each action in a thread happens-before every action in that thread that comes later in the program’s order.
  • An unlock on a monitor happens-before every subsequent lock on that same monitor.
  • A write to a volatile field happens-before every subsequent read of that same volatile.
  • A call to start method on a thread happens-before any actions in the started thread.
  • All actions in a thread happens-before any other thread successfully returns from a join method on that thread.

翻译:

  • 程序顺序规则:一个线程中, 每一个操作happens-before 于该线程中的任意后续操作。
  • 监视器锁规则:对监视器锁的一个解锁操作,happens-before 于随后对同一个监视器锁的加锁操作。
  • volatile变量规则:对一个volatile域的写操作,happens-before 于任意后面对这个volatile域的读操作。
  • 传递性规则:如果A happens-before B,且B happens-before C,那么A happens-before C。
  • 线程启动规则:Thread.start()的调用会happens-before于启动线程里面的动作。
  • 线程顺序规则:Thread中的所有动作都happens-before于其他线程从Thread.join中成功返回。
  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值