Volatile
Volatile
-
保证可见性
public class Test01 { // 不加 volatile 程序就会死循环! // 加 volatile 可以保证可见性 private volatile static int num = 0; public static void main(String[] args) throws ExecutionException, InterruptedException { new Thread(() -> { // 线程 1 对主内存的变化不知道的 while (num == 0) { } }).start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } num = 1; System.out.println(num); } } -
不保证原子性
原子性 : 不可分割
线程A在执行任务的时候,不能被打扰的,也不能被分割。要么同时成功,要么同时失败public class Test01 { // volatile 不保证原子性 private volatile static int num = 0; public static void add(){ num++; } /* synchronized 可保证原子性 public synchronized static void add(){ num++; }*/ public static void main(String[] args) { //理论上num结果应该为 2 万 for (int i = 1; i <= 20; i++) { new Thread(()->{ for (int j = 0; j < 1000; j++) { add(); } }).start(); } while (Thread.activeCount() > 2) { Thread.yield(); } System.out.println(Thread.currentThread().getName() + " " + num); } } -
如果不加 lock 和 synchronized ,怎么样保证原子性???
使用原子类,解决 原子性问题
public class Test01 {
// 原子类的 Integer
private volatile static AtomicInteger num = new AtomicInteger();
public static void add() {
num.getAndIncrement(); // AtomicInteger + 1 方法, CAS
}
public static void main(String[] args) {
//理论上num结果应该为 2 万
for (int i = 1; i <= 20; i++) {
new Thread(() -> {
for (int j = 0; j < 1000; j++) {
add();
}
}).start();
}
while (Thread.activeCount() > 2) {
Thread.yield();
}
System.out.println(Thread.currentThread().getName() + " " + num);
}
}
这些类的底层都直接和操作系统挂钩!在内存中修改值!Unsafe类是一个很特殊的存在!

浙公网安备 33010602011771号