简体   繁体   中英

using volatile keyword in java4 and java5

what is the difference in using volatile keyword in java4 and java5 onwards?

and related to that,

Read/write operations on non-atomic variables(long/double) are atomic when they are declared as volatile.

Is this also true for java4 or it is valid from java5 onwards???

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).
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. You can read this Double Checked Locking for example reference

This site gives a good explanation of the differences: 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

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.

And

Pre-Java 5, volatile was supposed to provide such guarantees for long and 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.

And this is from Java Language Specification,Second Edition :

17.4 Nonatomic Treatment of double and long

The load, store, read, and write actions on volatile variables are atomic, even if the type of the variable is double or long.

What is the difference in using volatile keyword in java4 and java5 onwards?

JMM before JDK5 is broken and using volatile for JDK4 may not provide the intended result. For more check this: 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.

Read/Write for long/double happen as two separate 32-bit operations. 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. In short read/write on long is not atomic operation unlike other primitives. 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. But again it may not work for JDK 4 or before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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