`
城的灯
  • 浏览: 150121 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java happen before(JSR133)

    博客分类:
  • 2013
阅读更多
相信对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中成功返回。
1
2
分享到:
评论
1 楼 城的灯 2013-09-22  
这个就很好的解释了软件包 java.util.concurrent.atomic支持在单个变量上解除锁的线程安全编程。

原子访问和更新的内存效果一般遵循以下可变规则,正如 The Java Language Specification, Third Edition (17.4 Memory Model) 中的声明:

get 具有读取 volatile 变量的内存效果。
set 具有写入(分配)volatile 变量的内存效果。
除了允许使用后续(但不是以前的)内存操作,其自身不施加带有普通的非 volatile 写入的重新排序约束,lazySet 具有写入(分配)volatile 变量的内存效果。在其他使用上下文中,当为 null 时(为了垃圾回收),lazySet 可以应用不会再次访问的引用。
weakCompareAndSet 以原子方式读取和有条件地写入变量但不 创建任何 happen-before 排序,因此不提供与除 weakCompareAndSet 目标外任何变量以前或后续读取或写入操作有关的任何保证。
compareAndSet 和所有其他的读取和更新操作(如 getAndIncrement)都有读取和写入 volatile 变量的内存效果。

相关推荐

Global site tag (gtag.js) - Google Analytics