Java memory model and threads (4): special rules and atomicity, visibility and ordering of long and double

The JVM divides the read and write operations of 64-bit data that are not modified by volatile into two 32-bit operations, that is, the non-atomic agreement between long and double, and multiple threads share a variable of type long or double. It is possible to read to a "half variable" value.

However, the above situation is very rare, so these two types do not need volatile modification.

The Java memory model defines three characteristics: atomicity, visibility, and ordering.

Atomicity, which means that the operations we talked about in the previous sections are atomic, either all succeed or all fail.

In addition to the volatile keyword for visibility, there are two other keywords that implement both synchronized and final visibility.

Synchronized modifies the visible line of the synchronized block. Performance tells that before performing the unlock operation on a variable, the variable must be synchronized to the main memory.

The visibility of final means that once the initialization of the constructor is completed, the constructor does not pass the this reference, and other threads can also see the final modified field value.

Orderly, that is to say, in this thread, all operations are in order. If an existing car observes another thread, many operations are out of order.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684406&siteId=291194637