相信对Java并发编程有所了解的人一定知道JMM,Java内存模型主要就是用来控制线程之间通信的,JMM决定了一个线程对共享变量的修改何时对别的线程可见。JMM定义了线程和主内存之间的抽象关系:线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地内存(local memory),本地内存中存储了该线程以读/写共享变量的副本。本地内存是JMM的一个抽象概念,并不真实存在。
从jdk5开始JSR133提出了新的内存模式即happen before的概念,通过找个概念阐述了多个操作的可见性。此处所说的是多个操作,而非多个线程,因为编译器重排序规则和处理器重排序,并非按照我们写的代码的顺序执行。那什么是happen before呢?最简单的就是一个操作的结果对另一个可见,可以简单粗暴的理解为什么在什么之间执行(实际上其实不是这样的)。
JSR133的目标:
Preserving existing safety guarantees, like type-safety, and strengthening others. For example, variable values may not be created "out of thin air": each value for a variable observed by some thread must be a value that can reasonably be placed there by some thread.
The semantics of correctly synchronized programs should be as simple and intuitive as possible.
The semantics of incompletely or incorrectly synchronized programs should be defined so that potential security hazards are minimized.
Programmers should be able to reason confidently about how multithreaded programs interact with memory.
It should be possible to design correct, high performance JVM implementations across a wide range of popular hardware architectures.
A new guarantee of initialization safety should be provided. If an object is properly constructed (which means that references to it do not escape during construction), then all threads which see a reference to that object will also see the values for its final fields that were set in the constructor, without the need for synchronization.
There should be minimal impact on existing code.
happen 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() 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.
happen before翻译过来就是:
程序顺序规则:一个线程中的每个操作,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中成功返回。
分享到:
相关推荐
主要给大家介绍了关于Java利用happen-before规则实现共享变量的同步操作的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
它的具体表现形式,包括但远不止是我们直觉中的 synchronized、volatile、lock 操作顺序等方面,例如:线程内执行的每个操作,都保证 happ
ERP Make it Happen ERP Make it Happen ERP Make it Happen ERP Make it Happen
本文讲了Java并发编程中volatile变量、happens-before与内存一致性错误,下面来和小编一起学习下如何解决
JAVA断言技术详解,1、What will happen when you attempt to compile and run the following code? (Assume that the code is compiled and run with assertions enabled) 1. public class AssertTest 2. { 3. ...
In the updated edition of this critically acclaimed and bestselling book Microsoft project veteran Scott Berkun offers a collection of essays on field tested philosophies and strategies for defining ...
一提到happens-before原则,就让人有点“丈二和尚摸不着头脑”。这个涵盖了整个JMM中可见性原则的规则,究竟如何理解,把我个人一些理解记录下来。下面可以和小编一起学习
去CSDN下载所谓破解出现以下错误:wrapper | Licensed to (null) for (null) wrapper | wrapper |... (6) jvm 1 | WrapperJNI Error: This can happen if the Wrapper binary is not... 自己改了之后可以部署了,分享包
初步认识 Volatile、从硬件层面了解可见性的本质、JMM、HappenBefore;初步认识 Volatile、从硬件层面了解可见性的本质、JMM、HappenBefore;
有关ERP和SAP的使用中的经典案例,语言简单易懂,非常利于ERP和SAP的学习。
$ npm install --save make-fetch-happen 目录 fetch.defaults minipass-fetch选项 make-fetch-happen选项 opts.cacheManager opts.cache opts.proxy opts.noProxy opts.ca, opts.cert, opts.key opts....
the learning can happen anytime and anyplace). The main rolls in these systems are the learners, the authors and the administrators. The authors create learning content by means of an Authority ...
微软项目管理入门教材,Scott Berkun
本文主要介绍Java高并发内存模型和线程安全的资料,这里整理详细的资料及1.原子性 2.有序性 3.可见性 4.Happen-Before 5.线程安全的概念,有需要的小伙伴可以参考下
This sounds bad, but it needs to happen if Java is to ultimately stay in the mainstream. That is, if feature accretion hasn't already irreparably damaged the language.
高中英语 知识点大全66 happen的用法
Crashes Happen - Downtime Won't with Data Guard
安装$ npm install --save make-fetch-happen 目录例子const fetch = require ( 'make-fetch-happen' ) . defaults ( { cacheManager : './my-cache' // path where cache will be written (and read)} )fetch ( '...
动词happen的语义韵和类联接的对比研究.pdf
Bug 19523291 Subquery unnesting does not happen(Doc ID 19523291.8).pdf