java - how volatile keyword Influences atomicity when defining a long or double variable -


I've been thinking lately in Java and confused with the following:

However, you receive atomism (for simple work and returns) if you use a volatile keyword while defining long or double variables (note that the volcano was not working properly before Java SE5).

Why can I get atoms if I use fluctuation while defining long or dual variables? JVM is allowed to write and write 64-bit quantities (long and double variables) as two separate 32-bit functions, and if I am unstable by two different 32-bit operation threading mechanisms as long or double variables Do not interrupt?

The entire paragraph content:

Atomicity is "simple operation" long and in addition to doubles, on the primitive types of undivided (atomic) operations, Reading and writing primitive characters other than dual, however, there may be a context switch between reading and writing JVM and after that, 64-bit quantities (long and double variables as two separate 32-bit operations) ) To read and write Numti, and individual functions can see the erroneous results (it is sometimes referred to as tearing word, because you can change its value, then it can change its value). However, if you use fluctuation while defining long or double variables, then you obtain atomism (for simple work and returns) (note that the volcano was not working properly before Java SE5). Different JVMs are free to offer a strong guarantee, but you should not rely on platform-specific features.

The volatile fields are written directly through the main memory, and are read from the main memory. So even I define the lengthy or dual variable as volatile, 32-bit operations can be and can change the long or double value in the main memory, at that time when the 32- Bit operations are there and the first 32-bit main memory can be the operation before the value changes.

In such cases it's best to just see JLS:

:

For the purposes of the Java Programming Language Memory Model, non-volatile long or double values ​​are considered as two separate writing types: each 32-bit half. This could be the case, where a thread looks at 32 bits before the 64-bit value by typing one, and another writes 32 bits.

The volatile writes long and double values ​​and always reads atoms.

Writes contexts and reads are always atom, even if they have been implemented as 32-bit or 64-bit values.

Note that "the volatile field is written through the main memory immediately, and is read from the main memory" is not really true, it is a possible implementation, but how the volcano In fact, given the guarantees actually implemented in JVM, its own decision is the other, better solutions based on the underlying hardware's Cash Cause protocol.

On the other hand it is conceivable that it will include some software lock to avoid reading / writing to guarantee a few low-level hardware. Yes, but JVM is completely free to do this.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -