The memory model of Java is divided into two types: primary memory and working memory. Main memory is the area where the instance is located. Main memory is shared for all thread locks, and each thread has its own separate work area, called working memory. When a thread needs to manipulate some of the fields of an object, it will copy the required information from the main memory to its own working memory. And this copy is called a working copy.
In this case, you can imagine how Java guarantees memory consistency. The content above the main memory is like a standard, and the working copy of each thread is like a draft. The calculated results on the draft will be put into main memory. So that other threads can see it, and that's visibility. When a thread obtains a lock on an object, he is given the right to change the state of the object, and he starts to copy the contents of main memory to his working memory. At this point, whatever changes he made to the working copy were not in main memory, he had to write the work copy to main memory when he wanted to release the lock. Why is it? He assumes that the content of main memory is not written, but only the other threads do not know that there is a thread to do something, can ignore AH. The problem is not to ignore, in order to ensure that every thread does things are seen by the thread behind him, he must release the lock before the content to write to main memory, even if his own work is finished only part.
This makes it possible to obtain the difference between the lock and the lock, and the thread that gets the lock first is visible to the thread that acquired the lock.
Of course, the thread does not simply have to release the lock when the working copy is written to main memory. He is able to write one time after the field of the object has been specified, and can finally write to the last value. determined by the processor.
The arguments and local variables of a method are unique to each thread, so synchronization is not a concern.
Multithreaded programming needs to solve two problems is the interference between the threads and memory inconsistency, synchronized here is the right advantage, its two role is "thread Synchronization" and "Memory synchronization." The method or block protected by synchronized is called a critical section, and only one thread can access it at the same time, like a single-plank bridge. And it's just a single-plank bridge where one person can go up.
Memory synchronization refers to the synchronization of the contents of the primary and working memory, to ensure that two content is the same.
When will it be synchronized? It is impossible to synchronize all the time, that all the threads are always looking at their own with main memory synchronization. Isn't that a waste of time? Thread will be in "want to enter synchronized block" and "want to exit synchronized block" is to synchronize, synchronization contains two aspects, one is to write their own work copy to main memory. One is reading content from main memory. When a thread enters synchronized, the contents of the working copy that are not written to main memory are forced to write to main memory, and the same forced write is made when exiting synchronized.
The difference is that the thread also frees the working memory when entering synchronized. This ensures that the new content is read, but will not release the work memory when you want to exit synchronized, after all, it is not considered to read the new content, in short, the next time you enter the synchronized block will be read.
This shows that main memory and working memory content is not always synchronized, and most of the time should be different. Memory synchronization is to ensure that the need to use this content and main memory is consistent. The content is guaranteed to be written to main memory after it has been changed.
In other words, make sure someone else writes what you see, and make sure you write it.
The difference between volatile and synchronized is that it only synchronizes memory and does not guarantee thread synchronization.
When a thread references the Valotile field, it starts from the main memory copy value to the working memory, and when the thread specifies the value into the volatile field, it will copy the value to main memory.
Volatile also has a function that guarantees the atomicity of the specified action on long and double. Specifying a basic data type such as int or bye is atomic, and only the specified long and double are divided into two times, which can result in dirty data in the event of multithreading. Therefore, in multithreaded programming must ensure that long and double the atomic access to ask.
Java memory model