Java concurrent programming (9) memory model, java model
JAVA Memory Model
In the multi-thread series, the underlying layer of the memory model is not explored.
I. What is the memory model and why does it need it?
In modern multi-core processors, each processor has its own cache and regularly coordinates with the main memory;
To ensure that each processor knows what other processors are doing at any time, it will require a lot of overhead, and it is usually unnecessary.
We only need to know the information when we need to share data across threads. in JAVA, the correct synchronization is implemented.
1. reorder
It is very difficult to judge the output value as follows:
Public class PossibleReordering {static int x = 0, y = 0; static int a = 0, B = 0;/*** it will be very difficult to judge the output value: * 1: switching between multiple threads leads to the possible output values: () * 2. command Re-sorting: one thread such as resorting between a = 1 and x = B, x = B (0), then the other thread is scheduled to execute y = a (0 ), will cause () */public static void main (String [] args) throws InterruptedException {Thread one = new Thread (new Runnable () {public void run () {a = 1; x = B ;}}); Thread other = new Thread (new Runnable () {public void run () {B = 1; y = a ;}}); one. start (); other. start (); one. join (); other. join (); System. out. println ("(" + x + "," + y + ")");}}
2. Introduction to the JAVA Memory Model
Partial Order relationship: opposition, inversion, and transmission attributes. However, for any two elements A and B, the relationship between A and B is not necessarily satisfied.
For example, I prefer B between A and B, but I do not need to make A clear choice.
JMM defines A partial order relationship for all operations in the program, which is called Happens-Before. To ensure that the thread that executes the B operation sees the result of the thread that executes the operation, whether or not AB is in the same thread, must satisfy the Happens-Before relationship, otherwise the JVM will reorder it
For example, the lock operation can predict the execution sequence. Multiple Threads are in line with Happens-Before. If no lock is applied, scheduling between threads cannot be determined,
3. Release
Real reason: There is no Happens-Before relationship between releasing a shared object and accessing the object in another thread. if the object is not correctly constructed due to Command Re-sorting, it will be released.
Public class UnsafeLazyInitialization {private static Resource resource;/*** in addition to the status condition check and execution, there are also insecure release issues * For example: A thread A comes in, if the resource is null, It is instantiated and returned. If the resource is not null in the other thread B, it is directly returned * If the resource is modified in thread, the correct status of resource may not be visible in thread B */public static Resource getInstance () {if (resource = null) resource = new Resource (); // unsafe publication return resource;} static class Resource {}}