简体   繁体   English

在java4和java5中使用volatile关键字

[英]using volatile keyword in java4 and java5

what is the difference in using volatile keyword in java4 and java5 onwards? 在java4和java5中使用volatile keyword什么区别?

and related to that, 与此相关,

Read/write operations on non-atomic variables(long/double) are atomic when they are declared as volatile. 非原子变量(long / double)的读/写操作在声明为volatile时是原子的。

Is this also true for java4 or it is valid from java5 onwards??? 这对java4也是如此, 或者从java5开始有效吗?

Yes there is a difference. 是,有一点不同。
Up to Java 4 volatile could be re-ordered by compiler with respect to any previous read or write, leading to subtle concurrency bugs eg making it impossible to implement a double check locking (very common idiom for a Singleton). 编译器可以针对任何先前的读取或写入重新排序Java 4 volatile ,从而导致细微的并发错误,例如使得无法实现double check锁定(Singleton的非常常见的习惯用法)。
This is fixed in Java 5.0 which extends the semantics for volatile which cannot be reordered with respect to any following read or write anymore and introduces a new memory model. 这在Java 5.0中得到修复,它扩展了volatile的语义,不再对任何后续的读或写进行重新排序,并引入了新的内存模型。 You can read this Double Checked Locking for example reference 您可以阅读此Double Checked Locking作为示例参考

This site gives a good explanation of the differences: http://www.javamex.com/tutorials/synchronization_volatile.shtml 这个网站很好地解释了这些差异: http//www.javamex.com/tutorials/synchronization_volatile.shtml

They also give an explanation of the behavior of volatile in Java 5 on a separate page: http://www.javamex.com/tutorials/synchronization_volatile_java_5.shtml 他们还在单独的页面上解释了Java 5中volatile的行为: http//www.javamex.com/tutorials/synchronization_volatile_java_5.shtml

People have provided good points and references responding to my question answering first part. 人们提出了好的观点和参考,回答了我的问题,回答了第一部分。

Going specific to the second part of question, this i read at some forum: 具体到问题的第二部分,我在一些论坛上读到:

A volatile declared long is atomic (pre-Java 5 also) in the sense that it guarantees (for all JVM implementations) a read or write go directly to main memory instead of two 32-bit registers. 挥发性声明long是原子的(也是Java 5之前的版本),因为它保证(对于所有JVM实现)读或写直接转到主内存而不是两个32位寄存器。

And

Pre-Java 5, volatile was supposed to provide such guarantees for long and double. 在Java 5之前,volatile 应该为long和double提供这样的保证。 However things did not work out this way in practice, and implementations frequently violated this guarantee. 然而事情并没有在实践中以这种方式发挥作用,并且实施经常违反此保证。 As I recall the issue seemed to get fixed around JDK 1.4, but as they were still working on the whole memory model thing, they didn't really make any clear announcements about it until JDK 5, when the new rules were announced, and memory guarantees actually meant something. 我记得这个问题似乎已经解决了JDK 1.4,但由于他们仍然在处理整个内存模型的事情,他们并没有真正做出任何关于它的明确公告,直到新规则宣布的JDK 5和内存保证实际意味着什么。

And this is from Java Language Specification,Second Edition : 这是来自Java语言规范,第二版

17.4 Nonatomic Treatment of double and long 17.4双和长的非原子疗法

The load, store, read, and write actions on volatile variables are atomic, even if the type of the variable is double or long. 对volatile变量的加载,存储,读取和写入操作都是原子的,即使变量的类型是double或long。

What is the difference in using volatile keyword in java4 and java5 onwards? 在java4和java5中使用volatile关键字有什么区别?

JMM before JDK5 is broken and using volatile for JDK4 may not provide the intended result. JDK5之前的JMM被破坏,并且对JDK4使用volatile可能无法提供预期的结果。 For more check this: http://www.ibm.com/developerworks/library/j-jtp02244/ 有关更多信息,请访问http//www.ibm.com/developerworks/library/j-jtp02244/

Read/write operations on non-atomic variables(long/double) are atomic when they are declared as volatile. 非原子变量(long / double)的读/写操作在声明为volatile时是原子的。

Read/Write for long/double happen as two separate 32-bit operations. 长/双读/写发生在两个独立的32位操作中。 For two threads it is possible that one thread has read higher 32-bits and other one has read lower 32-bits of a long/double variable. 对于两个线程,一个线程可能读取更高的32位,而另一个线程读取长/双变量的低32位。 In short read/write on long is not atomic operation unlike other primitives. 简而言之,读取/写入long不是原子操作,与其他原语不同。 Using volatile for long/double is supposed to provide such guarantee as the instructions for volatile are not re-ordered for volatile read/write by compiler and volatile also provides visibility guarantee. 使用volatile for long / double应该提供这样的保证,因为volatile的指令不会被编译器重新排序用于易失性读/写,而volatile也提供可见性保证。 But again it may not work for JDK 4 or before. 但同样,它可能不适用于JDK 4或之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM