Home > Java > javaTutorial > body text

The functions and differences between the keywords volatile and synchronized in Java

巴扎黑
Release: 2017-06-26 10:20:18
Original
1676 people have browsed it

volatile is a variable modifier, while synchronized acts on a piece of code or method ; the following three get method codes:

1 int i1;2 int geti1() {return i1;}3 4 volatile int i2;5 int geti2() {return i2;}6 7 int i3;8 synchronized int geti3() {return i3;}
Copy after login

geti1() gets the value of i1 stored in the current thread. Multiple threads have multiple copies of the i1 variable, and these i1s can be different from each other. In other words, another thread may have changed the i1 value in its thread, and this value may be different from the i1 value in the current thread. In the Java memory model, there is main memory (main memory area), which stores the current "accurate value" of variables. Each thread also has its own memory (such as registers). For performance reasons, a thread keeps a copy of the variables it accesses in its own memory. In this way, there will be situations where the value of the same variable in the memory of one thread may be inconsistent with the value of the memory of another thread or the value of main memory at a certain moment. Therefore, there is actually a possibility: the value of i1 in main memory is 1, the value of i1 in thread 1 is 2, and the value of i1 in thread 2 is 3. This changes their respective i1 values ​​in both thread 1 and thread 2. And this change will happen before it has time to be passed to main memory or other threads.

geti2() gets the i2 value of main memory. Declaring a variable as volatile means that the variable will be modified by other threads at any time, so it cannot be cached in thread memory. In other words, a variable must be synchronized in all threads after being modified by volatile. If its value is changed in any thread, all other threads immediately obtain the same value. Therefore, volatile-modified variables consume a little more resources than ordinary variables when accessing, because it is more efficient for the thread to have its own copy of the variable.

The geti3() method is modified by synchronized. When synchronized is used to modify a method or a piece of code, it can be guaranteed that at most one thread can execute the code at the same time. Since the volatile keyword has achieved data synchronization between threads, why do we need synchronized? When two concurrent threads access the synchronized(this) synchronization code in the same object object, only one thread can be executed at a time. Another thread must wait for the current thread to finish executing this code block before it can execute this code block. However, when one thread accesses a synchronized(this) synchronized code block of object, another thread can still access the non-synchronized(this) synchronized code block in the object. What's particularly critical is that when a thread accesses a synchronized(this) synchronized code block of object, other threads' access to all other synchronized(this) synchronized code blocks in object will be blocked. When a thread accesses a synchronized(this) synchronized code block of object, it obtains the object lock of this object. As a result, other threads' access to all synchronized code parts of the object is temporarily blocked.

Summarize the differences:

First, volatile is a variable modifier, while synchronized acts on a piece of code or method.

Second, volatile only synchronizes the value of a variable between thread memory and main memory (main memory); while synchronized synchronizes the value of all variables by locking and unlocking a monitor. Obviously synchronized consumes more resources than volatile.

The above is the detailed content of The functions and differences between the keywords volatile and synchronized in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template