Java Concurrency Programming--volatile/synchronized

Source: Internet
Author: User
Tags cas

First, Java Concurrency Foundation

When an object or variable can be shared by multiple threads, it is possible to cause problems with the logic of the program. There is a variable in an object i=0, there are two threads, a, a, I plus 1, this time there is a problem to appear, the key is that I plus 1 of the process is not atomic operation. To increment the I, the first step is to get the value of I, when a gets the value of I is 0, before a new value is written to a, B gets the value of a 0, then a writes, I becomes 1, then B also writes i,i this time remains 1. Of course, Java's memory model is not as simple as above, in the Java memory model, memory is divided into two categories, main memory and working memory,main memory for all threads shared, working Memory is stored in the thread required to copy the variables (thread to the main memory of the contents of the operation, the first need to copy to their own working memory, generally for speed, working memory is generally in the CPU cache). Volatile modified variables will not produce a copy of the working memory while being manipulated, but instead directly manipulate the main memory, although volatile solves the problem of variable visibility, but does not solve the problem of atomicity of variable operation, This also requires synchronized or CAS-related operations.

Before introducing the keywords commonly used in these two concurrent programming, let's introduce two concepts: visibility, atomicity

Visibility: 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. once a thread modifies the volatile modified variable, it guarantees that the modified value is immediately updated to main memory and that the modified value can be obtained immediately when another thread needs to read it. For normal variables: the read operation takes precedence over the data from the working memory, and if it does not exist in the working memory, copies a copy of the data from the main memory to the working memory; The write operation modifies only the copy data of the working memory, in which case the other threads cannot read the latest value of the variable. For volatile variables, the read operation JMM the corresponding value in the working memory to be invalid, requiring the thread to read the data from the main memory, while the write operation JMM the corresponding data in the working memory into main memory, in which case the other thread can read the latest value of the variable.

The use volatile volatile does not guarantee the atomicity of the composite operation, even though i++ it is actually composed of multiple atomic operations: read i; inc; write i If multiple threads are executing at the same time i++ , it volatile is only guaranteed that they operate on the i same piece of memory, However, there is still a possibility of writing dirty data.

atomicity: either do not execute or execute to the end. Atomicity is when a thread modifies the value of I, from removing I to the value of the new I to I no other thread can do anything to I. This means that the operation of a thread on I is atomic, so that the data can be prevented from being dirty read. Operations can be guaranteed by locking mechanisms or CAS (Compare and Set require hardware CPU support).


Summarize:

    1. The volatile nature is to tell the JVM that the value of the current variable in the register (working memory) is indeterminate and needs to be read from main memory, while the synchronized is locking the current variable, and other threads are blocked only if the variable is accessible to the thread.
    2. Volatile can only be used at variable levels; synchronized can be used in variables, methods, and class-level
    3. Volatile can only achieve the change of the visibility of variables, not guaranteed atomicity, while synchronized can guarantee the change of the visibility of variables and atomicity
    4. Volatile does not cause thread blocking, and synchronized can cause thread blocking.
    5. Volatile tagged variables are not optimized by the compiler; Synchronized tagged variables can be optimized by the compiler

Reference:

http://blog.csdn.net/suifeng3051/article/details/52611233

Https://www.jianshu.com/p/beb2c98003c4

Java Concurrency Programming--volatile/synchronized

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.