Using volatile keyword in java4 and java5

What is the difference in usage volatile keywordin java4 and java5 onwards?

and related

The read / write operations of non-atomic variables (long / double) are atomic when they are declared as mutable.

Is this also true for java4 or , is it really with java5 onwards ???

+5
source share
4 answers

People provided good points and recommendations, answering my question, answering the first part.

Turning to the second part of the question, I read on some forum:

A volatile declared long atomic (pre-Java 5 also) in the sense that it guarantees (for all JVM implementations) read or write directly to main memory instead of two 32-bit registers.

and

Pre-Java 5, volatile . , . , , ​​ JDK 1.4, , JDK 5, , -.

Java Language, Second Edition:

17.4

, , , .

+3

, .
Java 4 volatile , concurrency, . double check ( Singleton).
Java 5.0, volatile, . Double Checked Locking,

+4

volatile java4 java5 ?

JMM , JDK5 volatile JDK4 . : http://www.ibm.com/developerworks/library/j-jtp02244/

/ (long/double) , .

Read / write for long / double occurs as two separate 32-bit operations. For two streams, it is possible that one stream reads the higher 32-bit, and the other has the lower 32 bits of the long / double variable. In short, reading / writing in length is not an atomic operation, unlike other primitives. Using volatile for long / double should provide such a guarantee, since the instructions for volatile are not reordered for mutable read / write by the compiler, and volatile also provides a guarantee of visibility. But again, this may not work for JDK 4 or earlier.

0
source

All Articles