"Java Concurrency Programming": Two ways to implement memory-visible comparisons in concurrent programming: locking and volatile variables

Source: Internet
Author: User
Tags visibility

In this blog post, http://blog.csdn.net/ns_code/article/details/17288243 describes the way in which memory visibility is achieved synchronously, in http/ blog.csdn.net/ns_code/article/details/17101369 This blog post, describes the method of using volatile variables to achieve memory visibility, which compares the difference between the two.

1, volatile variable is a slightly weaker synchronization mechanism does not perform the lock operation when accessing the volatile variable, so the execution thread is not blocked, so the volatile variable is a more lightweight synchronization mechanism than the Synchronized keyword.

2. From the perspective of memory visibility, writing a volatile variable is equivalent to exiting a synchronous code block, while reading a volatile variable is equivalent to entering a synchronous code block.

3. It is often more brittle and more difficult to understand the code if it relies too much on volatile variables to control the visibility of the state. This should only be used if the volatile variable can simplify the implementation of the Code and validate the synchronization policy. In general, it is more secure to use the synchronization mechanism.

4, locking mechanism (i.e. synchronization mechanism) can ensure both visibility and atomicity, and volatile variables can only ensure visibility, because simple variable declared volatile if the current value is related to the previous value of the variable, then the volatile keyword does not work, meaning the following expression is not an atomic operation: " count++ "," Count = count+1 ".

You should use a volatile variable only if all of the following conditions are true:

1, the write operation to the variable does not depend on the current value of the variable, or you can ensure that only a single thread updates the value of the variable.

2. The variable is not contained in an invariant that has other variables.


Summary: When you need to sync, the first choice should be the Synchronized keyword, which is the safest way to try any other way is risky. in particular, after the jdK1.5, the synchronized synchronization mechanism has done a lot of optimization, such as: self-adaptive spin lock, lock coarsening, lock elimination, lightweight lock, etc., so that its performance has significantly improved.

"Java Concurrency Programming": Two ways to implement memory-visible comparisons in concurrent programming: locking and volatile variables

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.