Volatile不能保證原子性
阿新 • • 發佈:2021-07-08
- 併發程式設計的三大特性 :可見性 原子性 有序性
- volatile保證可見性和有序性,但不能保證原子性,保證原子性需要藉助synchronize這樣的鎖機制
public class ThreadSychronizedTest { public static volatile int num = 0; public static void increase() { num++; } public static void main(String[] args) throws InterruptedException { Thread[] threads
底層執行過程