Java concurrency: Volatile keywords, Atomic operations Atomic, volatileatomic

Source: Internet
Author: User

Java concurrency: Volatile keywords, Atomic operations Atomic, volatileatomic

Volatile keywords

Volatile is a special modifier that can only be used by member variables. Compared with the mutual exclusion provided by Synchronized and ReentrantLock, Synchronized ensures the visibility of variables in Synchronized synchronization blocks, volatile ensures the visibility of the modified variables. Visibility refers to the value that can be seen in other threads after a variable value is modified in one thread (when Java concurrent programs do not have a synchronization class, multi-thread operations on member variables are transparent (invisible) to other threads )). Volatile only ensures the visibility of the same variable in multiple threads, so it is used to modify the variable used as the switch state.

The java keyword volatile, on the surface, indicates that the variable is variable and unstable. In fact, the keyword is used to tell the compiler, all variables declared by this keyword are changeable and unstable. Therefore, do not try to use caching and other optimization mechanisms for this variable, but read the value from its memory address every time. Variables marked with volatile do not need to be locked when they are read or written. This reduces the probability of deadlock and keeps the code concise.

Note that it only means that each time you read the volatile variable, you must read it from its memory address,It does not mean that the value of volatile should be immediately written back to the memory every time the variable is modified.. That is to say, volatile only provides memory visibility, but does not provide atomicity. operations are mutually exclusive and provide the overall atomicity of operations, the visibility between multiple threads of the same variable and operations in multiple threads are mutually exclusive. Therefore, it is unreliable to use this keyword for High-concurrency security mechanisms.

Volatile is used as follows:

Public volatile static int count = 0; // Add the volatile keyword to the Declaration.

When will the volatile keyword be used? When we know the role of volatile, we also know where it should be used. Obviously, it is better to have only one thread modify the variable and multiple threads read the variable. That is, the memory visibility requirements are high, and the atomicity requirements are low.

From the above description, we can see that the major difference between volatile and locking mechanism is that the locking mechanism can ensure both visibility and atomicity, while the volatile variable only ensures visibility.

 

Atomic operation Atomic

The Volatile variable ensures the first relation and ensures that the next read operation will occur after the previous write operation (that is, the write operation will occur before the subsequent read operation), but it cannot guarantee atomicity. For example, if you use volatile to modify the count variable, the count ++ operation is not atomic.

 

 

References:

This article briefly describes the volatile keywords and atomic operations of atomic in java concurrency. Here we post some excellent articles for readers to read.

(1) http://www.importnew.com/18126.html

(2) http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html

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.