The difference between volatile and synchronize

Atomicity (Atomicity) and visibility (visibility)

Atomic means that one moment, only one thread can execute a piece of code , the code via a monitor object protection. Thereby preventing multiple threads in conflict when updating shared state.
Visibility is more subtle, it must ensure that the changes before releasing the lock on the shared data is then made to obtain the lock another thread is visible. Without this synchronization mechanism to ensure the visibility provided by the thread of shared variables may see the value of pre-modification or inconsistent, which will lead to many serious problems.

volatile

It modifies the variable does not retain a copy, direct access to the main memory of.

In the Java memory model, there are main memory, each thread has its own memory (for example, register). For performance, a thread will keep the copy of the variable to be accessed in their memory in. In such a moment, the value of a thread in the memory of the possible values of the same variable with another thread will appear in the memory or the main memory in the case of inconsistent values . A variable declared as volatile, it means that this variable is expected to be modified at any time other threads, so it can not be in the cache memory in the thread.

scenes to be used

You can only use volatile variables instead of locking in some cases limited. For volatile variable provides an ideal thread-safe, you must meet the following two conditions:
1) write to the variable does not depend on the current value.
2) The tag is not included in the invariant with other variables.

volatile write the most suitable one thread, multiple threads read occasion.
If there are multiple threads concurrently write, still need to use a lock or a thread-safe container or atomic variables instead.

synchronized

When it is used to modify a method or a block of time, it is possible to ensure that at most one thread is executing code in the same period of the time .

  1. When the object is the same object in two concurrent threads to access this synchronized (this) synchronized block, a time only one thread to be implemented. Another thread must wait for the current thread to execute the code block after the completion of the implementation of the code block.
  2. However, when a thread to access the object of a synchronized (this) synchronized block, another thread can still access the object in a non-synchronized (this) synchronized block.
  3. Especially critical it is, when a thread to access the object of a synchronized (this) synchronized block, another object of all other threads synchronized (this) to access the synchronization code block will be blocked.
  4. When a thread to access the object of a synchronized (this) sync block, it is obtained that the object of the object lock. As a result, other threads to access all objects of the object synchronization code section have been temporarily blocked .

the difference

  1. volatile modifier is variable, acting on the synchronized piece of code or method .
  2. volatile memory and only thread "main" value of a variable sync between memory; and synchronized by locking and unlocking the value of synchronized all the variables of a monitor, apparently synchronized volatile than consume more resources.
  3. volatile will not cause obstruction thread; synchronized may cause obstruction thread.
  4. ensure visibility volatile data, but can not guarantee atomicity ; atomicity can be guaranteed and synchronized, to ensure visibility or indirectly, because it will private data memory in the common memory and to make synchronization.
  5. Variable volatile flag will not be optimized compiler; mark may be synchronized variable optimizing compiler.

Thread safety and visibility of atoms comprising two aspects, Java synchronization mechanisms are built around these two aspects to ensure thread-safe.
Where the main use of the volatile keyword in multiple threads can perceive instance variables are modified, and can get the latest value used is, you can get the latest value when using multiple threads reading the shared variable.

Keyword volatile prompt each thread reads from the shared memory variable instead of reading private memory, thus ensuring the visibility of synchronous data. But note that: If you modify the data in the instance variable

For example: i ++, i.e. i = i + 1, then such an operation is actually not an atomic operation, it is not thread safe. I ++ expression decomposition steps as follows:
1) i takes a value from memory.
2) Calculated value of i;
3) the value of i is written to memory.
If the time calculated in step worth 2, another thread can modify the value of i, name at this time the read data will appear dirty. The solution is to use the synchronized keyword. So the process itself is not volatile atomic data, but in time force the read and write data to main memory effect.

Reference:
https://www.jianshu.com/p/0a6ca9c7695b (JAVA clearance problems -Volatile and the Synchronize)
https://blog.csdn.net/it_manman/article/details/79497807 (the difference between volatile and synchronize a)
HTTPS: //blog.csdn.net/mccand1234/article/details/52139319 (the Java memory model)
https://www.cnblogs.com/hrlizhi/p/9418009.html (multithreading volatile keyword)
HTTPS: // Blog. csdn.net/vking_wang/article/details/9982709 (volatile applicable scenes)
https://www.jianshu.com/p/c6f190018db1 (instruction reordering)

Guess you like

Origin blog.csdn.net/mccand1234/article/details/91345168