线程同步: volatile
并发可见性问题
static boolean stop = false;
new Thread(() -> {
// 线程1 执行
System.out.println("线程1 is running...");
// 线程1 等待线程2将 stop状态改为true,然后停止执行
while (!stop) ;
// 然后输出
System.out.println("线程1 is terminated.");
}).start();
Thread.sleep(10);
new Thread(() -> {
// 线程2 执行
System.out.println("线程2 is running...");
// 线程2 将状态改为true,此时线程1应该停止执行
stop = true;
// 线程2 执行
System.out.println("线程2 B is terminated.");
}).start();线程之间不可见

volatile保证可见性
synchronized保证可见性
CPU MESI
指令重排序
单例模式-双重检查锁
读屏障、写屏障
最后更新于