【java】volatile关键字疑云,到底怎么用?

差别很小的4段代码,结果却截然不同

代码1:

public class RunThread extends Thread {
    /** volatile */
    private boolean isRunning = true;
    // private volatile int i = 0;
    private int i = 0;

    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println("进入 run() 方法中...");
        while (isRunning == true) {
            // System.out.println("1");
            i++;
        }
        System.out.println("线程结束了...");
    }

    public static void main(String[] args) throws InterruptedException {
        RunThread myThread = new RunThread();  //创建线程
        myThread.start();   //启动线程,run函数开始跑
        Thread.sleep(3000);
        myThread.setRunning(false);   //主线程修改变量值
        System.out.println("isRunning 的值已经设置为了 false");
        Thread.sleep(1000);
        System.out.println(myThread.isRunning);
    }
}
//运行结果: 非预期,陷入死循环
进入 run() 方法中...
isRunning 的值已经设置为了 false
false

代码2:

public class RunThread extends Thread {
    /** volatile */
    private boolean isRunning = true;
    private volatile int i = 0;
    // private int i = 0;

    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println("进入 run() 方法中...");
        while (isRunning == true) {
            // System.out.println("1");
            i++;
        }
        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);
    }
}
//运行结果: 符合预期
进入 run() 方法中...
isRunning 的值已经设置为了 false
线程结束了...
false

代码3:

public class RunThread extends Thread {
    /** volatile */
    private boolean isRunning = true;
    // private volatile int i = 0;
    // private int i = 0;

    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println("进入 run() 方法中...");
        while (isRunning == true) {
            System.out.print("1");
            // i++;
        }
        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);
    }
}
//运行结果: 符合预期
...(省略数万个1)1111111111111isRunning 的值已经设置为了 false
线程结束了...
false

代码4:

public class RunThread extends Thread {
    /** volatile */
    private boolean isRunning = true;
    // private volatile int i = 0;
    // private int i = 0;

    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println("进入 run() 方法中...");
        while (isRunning == true) {
            // System.out.print("1");
            // i++;
        }
        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);
    }
}
//运行结果: 不符合预期, 陷入死循环
进入 run() 方法中...
isRunning 的值已经设置为了 false
false

所以,如上4段代码到底有什么区别?

如上代码,没有打印线程结束了...就表示陷入了死循环,反之表示退出了循环。

网上搜到的文章,都举最后一段代码类似的例子,while循环里什么都不做,但是如果在while里做点事儿,那就跟做的事儿有关系了,具体跟做的事儿有什么关系,有没有大佬知道的?来说一下啊!

不用volatile也能同步的情形

看看大兄弟的解析,看起来比较有道理!能解部分疑惑!
https://blog.csdn.net/tianzhonghaoqing/article/details/124690987

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值