zoukankan      html  css  js  c++  java
  • volatile 的一个经典例子

    volatile 关键字的两层语义

    一旦一个共享变量(类的成员变量、类的静态成员变量)被 volatile 修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其他线程来说是立即可见的。 2)禁止进行指令重排序

    方式一:变量不使用 volatile 修饰

    public class VolatileTest extends Thread {
        private static boolean flag = false;
    
        public void run() {
            while (!flag) ;
        }
    
        public static void main(String[] args) throws Exception {
            new VolatileTest().start();
            Thread.sleep(2000);
            flag = true;
        }
    }
    

    方式二:变量使用 volatile 修饰

    public class VolatileTest extends Thread {
        private static volatile boolean flag = false;
    
        public void run() {
            while (!flag) ;
        }
    
        public static void main(String[] args) throws Exception {
            new VolatileTest().start();
            Thread.sleep(2000);
            flag = true;
        }
    }
    

    运行结果
    方式一:线程不会结束

    方式二:线程会结束

    public class TestMain {
        public static volatile boolean flag = true;
    
        public static void main(String[] args) throws InterruptedException {
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (flag) {
                    }
                    System.out.println(Thread.currentThread().getName() + "线程停止,死循环被打开");
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    flag = false;
                    System.out.println(Thread.currentThread().getName() + "修改 flag 为" + flag);
                }
            }).start();
    
            Thread.sleep(Integer.MAX_VALUE);
        }
    }
    

    public class RunThread extends Thread {
        /** volatile */
        private volatile boolean isRunning = true;
    
        private void setRunning(boolean isRunning) {
            this.isRunning = isRunning;
        }
    
        public void run() {
            System.out.println("进入 run() 方法中...");
            while (isRunning == true) {
                // doSomething()
            }
            System.out.println("线程结束了...");
        }
    
        public static void main(String[] args) throws InterruptedException {
            RunThread myThread = new RunThread();
            myThread.start();
            Thread.sleep(3000);
            myThread.setRunning(false);
            System.out.println("isRunning 的值已经设置为了 false");
            Thread.sleep(1000);
            System.out.println(myThread.isRunning);
        }
    }
    

    public class RunThread extends Thread {
        /** volatile */
        private boolean isRunning = true;
    
        private void setRunning(boolean isRunning) {
            this.isRunning = isRunning;
        }
    
        public void run() {
            System.out.println("进入 run() 方法中...");
            while (isRunning == true) {
                // doSomething()
            }
            System.out.println("线程结束了...");
        }
    
        public static void main(String[] args) throws InterruptedException {
            RunThread myThread = new RunThread();
            myThread.start();
            Thread.sleep(3000);
            myThread.setRunning(false);
            System.out.println("isRunning 的值已经设置为了 false");
            Thread.sleep(1000);
            System.out.println(myThread.isRunning);
        }
    }
    

  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序
    最新广商小助手 项目进展 OpenGL ES 3D在我项目中引用 代码太多只好选重要部分出来
    最后冲刺 我的项目 广商小助手
    最新一课 老师指点用Listview适配器
    安卓小学生四则运算
    大三上学期安卓一边学一边开始做一个自己觉得可以的项目 广商小助手App 加油
    我要再接再力 学更多
    用场景来规划测试工作
    阅读第13,14,15,16,17章
  • 原文地址:https://www.cnblogs.com/hgnulb/p/9942486.html
Copyright © 2011-2022 走看看