6.3 volatile特性
6.3.1 保证可见性
保证不同线程对某个变量完成操作后结果及时可见,即该共享变量一旦改变所有线程立即可见
不加volatile,没有可见性,程序无法停止
加了volatile,保证可见性,程序可以停止
- public class VolatileSeeDemo {
-
- /**
- * t1 -------come in
- * main 修改完成
- * t1 -------flag被设置为false,程序停止
- */
- static volatile boolean flag = true;
-
- public static void main(String[] args) {
- new Thread(() -> {
- System.out.println(Thread.currentThread().getName() + "\t-------come in");
- while (flag) {
-
- }
- System.out.println(Thread.currentThread().getName() + "\t-------flag被设置为false,程序停止");
- }, "t1").start();
-
- try {
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- //更新flag