The difference between reserved word volatile and synchronized in Java

Source: Internet
Author: User
Tags volatile

Source: http://blog.csdn.net/fanaticism1/article/details/9966163# 1, the lock provides two main features: Mutual exclusion (mutual exclusion) and visibility (visibility). Mutual exclusion allows only one thread to hold a particular lock at a time, so you can use this attribute to implement a coordinated access protocol to shared data so that only one thread can use that shared data at a time. Visibility is more complex, and it must ensure that the changes made to the shared data before the lock is released are visible to the other thread that subsequently acquired the lock-if there is no guarantee of this visibility provided by the synchronization mechanism, the shared variables that the thread sees may be pre-modified or inconsistent values, which can cause many serious problems. 2.

In Java, there are two ways to ensure data consistency when you read and write data in multiple threads:

Synchronous

Use the Synchronized keyword or lock object.

Volatile

Using the volatile keyword
To summarize volatile in a sentence, it enables the variable to be known to other threads as soon as the value changes.

Volatile explanation

First of all, we have to realize that there is such a phenomenon, the compiler in order to speed up the program, the write operation of some variables will be in the register or the CPU cache before the last write memory.
In this process, the new value of the variable is not visible to other threads. The effect of volatile is that the read and write operation of the variable it modifies must be in memory!

Volatile and synchronized

The volatile nature is to tell the JVM that the value of the current variable in the register is indeterminate, that it needs to be read from main memory, that synchronized locks the current variable, and that the other threads are blocked only if the variable is accessible to the front-thread.
Volatile can only be used at variable levels, synchronized can be used in variables, methods.
Volatile can only implement variable visibility, but does not have atomic characteristics, while synchronized can guarantee the change of visibility and atomicity of variables.
Volatile does not cause a thread to block, and synchronized can cause a thread to block.
Volatile tagged variables are not optimized by the compiler, and synchronized tagged variables can be optimized by the compiler.

3, therefore, in the use of volatile keywords to be cautious, not as long as the simple type variable using the volatile modifier, all operations on this variable is the original operation, when the value of the variable by its own last decision, such as n=n+1, n++, etc., The volatile keyword is invalidated only if the value of the variable is independent of its previous value, and the action on that variable is atomic level, such as n = m + 1, which is the original level. So when using volatile key must be cautious, if you are not sure, you can use synchronized instead of volatile.

Summary: The volatile nature is to tell the JVM that the value of the current variable in the register is indeterminate and needs to be read from main memory. Some of the effects of synchronized can be achieved, but when the n=n+1,n++ and so on, the volatile keyword will be invalidated, unable to play the same role as synchronized thread synchronization effect.

The difference between reserved word volatile and synchronized in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.