当前位置: 首页 > article >正文

【Java】volatile-内存可见性问题

1、什么是内存可见性问题?

(1)实例

要明白什么是内存可见性,我们首先来看一段代码

public class demo1 {
    public static int isQuit = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            while (isQuit == 0){

            }
            System.out.println("t1线程结束");
        });
        thread1.start();

        Thread thread2 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            isQuit = scanner.nextInt();
            System.out.println("isQuit已被修改");
        });
        thread2.start();
    }
}

运行结果如下

打开jconsole,查看thread1状态,发现thread1还在运行状态,也就是说thread1中的while循环还一直在继续

为什么会这样呢?

(2)内存可见性问题 

在计算机中,CPU在读取寄存器时比读取内存时快很多,这时编译器会对代码进行自动优化

在上述代码中,由于while循环中没有代码,线程1不断地读取isQuit的值进行判断,操作过于频繁

因此,编译器在第一次读取isQuit的值后,便将其存放在寄存器中,后续不再读取

导致我们在线程2里对isQuit 的值进行了修改,线程1也不能察觉

由此便产生了内存可见性问题

2、volatile

要解决内存可见性问题,我们首先想到的是使用Java中的关键字volatile

在变量前加上关键字volatile修饰,变量便不会再被编译器优化,进而就不会产生内存可见性问题

public volatile static int isQuit = 0;

完整代码如下

import java.util.Scanner;

public class demo2 {
    public volatile static int isQuit = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            while (isQuit == 0){

            }
            System.out.println("t1线程结束");
        });
        thread1.start();

        Thread thread2 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            isQuit = scanner.nextInt();
            System.out.println("isQuit已被修改");
        });
        thread2.start();
    }
}

最终运行结果

可见,内存可见性问题可用volatile关键字来解决

3、其他解决办法

 之所以产生内存可见性问题,是由于读取操作太过于频繁。只要我们降低读取的频率,同样也可以解决内存可见性问题

如在线程1中的while循环中加上sleep操作

import java.util.Scanner;

public class demo3 {
    public static int isQuit = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            while (isQuit == 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t1线程结束");
        });
        thread1.start();

        Thread thread2 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            isQuit = scanner.nextInt();
            System.out.println("isQuit已被修改");
        });
        thread2.start();
    }
}

运行结果


http://www.kler.cn/news/136565.html

相关文章:

  • Docker:容器化的革命
  • 如何接受Date范围的数据
  • 【进阶OpenCV】 (19)-- Dlib库 --人脸表情识别
  • JAVA Maven 的安装与配置
  • 【记录】VSCode|自用设置项
  • 【Python爬虫实战】使用BeautifulSoup和Scrapy抓取网页数据!
  • 云原生周刊:Istio 1.20.0 发布 | 2023.11.20
  • 图的基础知识(数据结构)
  • buildadmin+tp8表格操作(5)自定义组装搜索的查询
  • Linux驱动开发——块设备驱动
  • C语言编程陷阱 (九)
  • 深入解析序列模型:全面阐释 RNN、LSTM 与 Seq2Seq 的秘密
  • 用不用Microsoft Defender是你的自由,但不用最好也得有替代品
  • Java,集合框架,关于Collection接口(子接口List和Set)
  • Cloud 微服务
  • Redis篇---第十一篇
  • 2023年亚太杯数学建模亚太赛ABC题思路资料汇总贴
  • HttpClient发送MultipartFile多文件及多参数请求
  • 担忧CentOS停服?KeyarchOS系统来支撑
  • 大模型LLM 在线量化;GPTQ\AWQ量化及推理
  • Windows 安装 Docker
  • PostgreSQL数据库结合内网穿透实现公网远程连接
  • 2023年中职“网络安全“—Linux系统渗透提权③
  • 如何快速将txt类型的日志文件转换为excel表格并进行数据分析报表统计图(如:饼图、折线图、柱状图)?
  • WPS或Excel查找A列中有B列没有的值
  • synchronized锁膨胀过程