Understand the role of volatile in Java: to ensure the visibility and orderliness of data between multiple threads, specific code examples are needed
In Java multi-thread programming, in order to To ensure data synchronization between multiple threads, we often need to use the volatile keyword. The volatile keyword can ensure visibility and ordering, ensuring that multiple threads' read and write operations on a certain variable are correct.
1. Visibility
In a multi-threaded environment, if one thread modifies a shared variable, it is uncertain whether other threads can immediately see the modification result. It's a matter of visibility.
By using the volatile keyword, you can ensure that writes to volatile variables are visible to other threads. That is, when one thread modifies the value of a volatile variable, other threads can immediately see the modified value.
The following is an example:
public class VolatileDemo {
private volatile boolean flag = false; public void writeFlag() { flag = true; } public void readFlag() { while (!flag) { // 无限循环,等待flag变为true } System.out.println("flag is true"); } public static void main(String[] args) { VolatileDemo demo = new VolatileDemo(); Thread writeThread = new Thread(() -> { demo.writeFlag(); }); Thread readThread = new Thread(() -> { demo.readFlag(); }); writeThread.start(); readThread.start(); }
}
In the above example, we created a VolatileDemo class and defined A variable flag of volatile boolean type is obtained. The writeFlag method is used to set the flag to true, and the readFlag method is used to output a piece of information after waiting for the flag to become true.
In the main method, we create two threads, one thread executes the writeFlag method, and the other thread executes the readFlag method. Since flag is a volatile variable, when the writeFlag method sets the flag to true, the readFlag method will immediately see this modification.
2. Orderliness
In a multi-threaded environment, even if a shared variable is visible, the order in which multiple threads operate on this variable cannot be determined. This is because Sequential issues.
By using the volatile keyword, you can ensure that the read and write operations of volatile variables are executed in the order of the code. That is to say, when one thread modifies the value of a volatile variable, other threads will immediately see the modification, and the modification will be executed in the order of the code.
The following is an example:
public class VolatileDemo {
private volatile int count = 0; public void increaseCount() { count++; } public void printCount() { System.out.println(count); } public static void main(String[] args) { VolatileDemo demo = new VolatileDemo(); Thread increaseThread = new Thread(() -> { for (int i = 0; i < 1000; i++) { demo.increaseCount(); } }); Thread printThread = new Thread(() -> { for (int i = 0; i < 10; i++) { demo.printCount(); } }); increaseThread.start(); printThread.start(); }
}
In the above example, we created a VolatileDemo class and defined A volatile int type variable count. The increaseCount method is used to increase count by 1, and the printCount method is used to output the value of count.
In the main method, we create two threads, one thread executes the increaseCount method, and the other thread executes the printCount method. Since count is a volatile variable, when the increaseCount method modifies the value of count, the printCount method will immediately see the modification, and the modification will be executed in the order of the code.
Summary:
By using the volatile keyword, we can ensure the visibility and ordering of shared variables between multiple threads. In some cases, we may need to use the synchronization mechanism provided by other Thread classes, such as the synchronized keyword or Lock interface, to implement more complex multi-threaded operations. However, in simple multi-threaded scenarios, visibility and ordering issues can be solved well using the volatile keyword.
The above is the detailed content of Understand the role of volatile in Java: ensuring the visibility and orderliness of data between multiple threads. For more information, please follow other related articles on the PHP Chinese website!