Home  >  Article  >  Java  >  Things to note when using the volatile keyword in Java

Things to note when using the volatile keyword in Java

高洛峰
高洛峰Original
2017-02-27 15:20:171695browse

Volatile variables have synchronized visibility properties, but do not have atomic properties. This means that threads can automatically discover the latest value of volatile variables. Volatile variables can be used to provide thread safety, but only for a very limited set of use cases: there are no constraints between multiple variables or between the current value and modified value of a variable. Therefore, volatile alone is not sufficient to implement counters, mutexes, or anything with invariants related to multiple variables.

The volatile keyword is a slightly weaker synchronization mechanism in Java. Why is it called a weak mechanism.

Before understanding this, let’s first take a look at the two mechanisms that Java must comply with when synchronizing:

1. Visibility: When one thread modifies a shared variable, another The thread can read this modified value.

2. Atomicity: refers to indivisibility. Here it means that during the execution of the program, if an operation cannot be interrupted, it is an atomic operation.

The volatile keyword cannot guarantee atomicity, but only visibility. Therefore, under normal circumstances, this keyword cannot be used for synchronization operations. However, it can be used for synchronization when the following two specific conditions are met:

1. The operation result does not depend on the current value of the variable, or it can be guaranteed that only a single thread modifies the value of the variable.

2. Variables do not need to participate in invariant constraints with other state variables

Example:

volatile boolrean flag;
public void close(){
    flag = true;
}
public void open(){
   while(!flag){
    ...
   }
}

At this time The execution of the open() method depends on the value of the flag, and synchronization must be used. However, it is cumbersome to use the synchronized keyword or other methods to synchronize. In this case, the scenario meets the above two conditions, so the volatile keyword can be used for synchronization.

volatile keyword principle:

Variables modified by volatile will not be cached in registers or other places. Each write is written directly to the main memory, and the same is true for reads. Read from main memory. So visibility is guaranteed.

The above are the precautions for using the volatile keyword in Java introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!


For more related articles on precautions for using volatile keywords in Java, please pay attention to the PHP Chinese website!


Statement:
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