The difference between volatile and synchronized

Source: Internet
Author: User

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. (race condition)

2, in Java, in order to ensure the data consistency when the multithreading read and write data, can be used in two ways:

Sync: Use synchronized keyword, or lock object

Use the volatile keyword: to summarize volatile in a nutshell, it enables the variable to be known to other threads as soon as the value changes.

3, volatile detailed

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 operations of the variables it modifies must be in memory!

4.volatile and synchronized

1) 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, synchronized is locking the current variable, and the other threads are blocked only if the variable is accessible by the front-thread.
2) volatile can only be used at variable levels, synchronized can be used in variables, methods.
3) volatile can only achieve the change of the visibility of variables, but does not have atomic characteristics, and synchronized can be guaranteed to modify the variable visibility and atomicity.

 The Java programming idea says that when you define a long or double variable, you get (simple assignment and return operations) atomicity If you use the volatile keyword
4) volatile does not cause a thread to block, and synchronized may cause a thread to block.
5) Volatile tagged variables are not optimized by the compiler, and synchronized tagged variables can be optimized by the compiler.

5, when the value of a domain depends on its previous value, volatile will not work, such as n=n+1,n++. If the value of a domain is limited by the values of other domains, then volatile does not work, such as the lower and upper boundaries of the range class, and must follow the lower<=upper limit. The only safe case where volatile is used instead of synchronized is that there is only one mutable field in the class.

6, the first choice should be synchronized keyword.

The difference between volatile and synchronized

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.