当前位置:网站首页>volatile 关键字
volatile 关键字
2022-04-21 13:51:00 【linjun20】
作用:
1.线程可见
如果多个线程都使用了同一个变量,那么其中一个线程改变了该变量,其他线程都会读取到该变量的最新值。加上volatile关键字,强制线程每次读取该值的时候都去“主内存”中取值。
2.防止指令重排序
class ThreadVolatileDemo extends Thread {
public volatile boolean flag = true;
@Override
public void run() {
System.out.println("开始执行子线程....");
while (flag) {
}
System.out.println("线程停止");
}
public void setRuning(boolean flag) {
this.flag = flag;
}
}
public class ThreadVolatile {
public static void main(String[] args) throws InterruptedException {
ThreadVolatileDemo threadVolatileDemo = new ThreadVolatileDemo();
threadVolatileDemo.start();
Thread.sleep(3000);
threadVolatileDemo.setRuning(false);
System.out.println("flag 已经设置成false");
Thread.sleep(1000);
System.out.println(threadVolatileDemo.flag);
}
}
版权声明
本文为[linjun20]所创,转载请带上原文链接,感谢
https://blog.csdn.net/linjun20/article/details/121197247
边栏推荐
- Introduction and practice of automatic monitoring system Prometheus & grafana
- Zabbix5系列-监控mysql(5.7/5.8/mariadb) (十)
- 深度学习与图像识别:原理与实践 笔记Day_17
- networkx与PyG计算度数degree时需避免的坑:自环selfloop和多重边
- networkx计算边的重要性:边介数或者中介中心性edge_betweenness
- Impala common commands (continuous updating)
- word2vec和node2vec笔记(更新ing)
- 各种排序的复习笔记
- Esgyndb about the performance improvement of delete with index
- jupyter notebook 如何跨文件复制多个代码块/cells (复制到另外一个文件)
猜你喜欢

Zabbix5系列-接入Grafana面板 (十七)

Zabbix5系列-监控惠普服务器iLO管理口 (六)

Shandong University project training raspberry pie promotion plan phase II (VII) objects and categories

iscsi

<2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(十二)

Why should sparse adjacency matrix be written in transposed form adj in pytorch geometric_ t

Pits to be avoided when Networkx and pyg calculate degree degree: self loop and multiple edges

Zabbix5系列-监控mysql(5.7/5.8/mariadb) (十)

<2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(六)

Software engineering - Fundamentals
随机推荐
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (6)
电力系统相关知识
pytorch geometric中为何要将稀疏邻接矩阵写成转置的形式adj_t
Oracle 备份与用户解锁
2021-10-19性能测试
并发编程之JMM模型和并发三大特性
山东大学项目实训树莓派提升计划二期(五)book1内容分析与整理
Number of objects in esgyndb SQL statistics mode
2021-10-20接口测试
Trim function of esgyndb
集合线程安全
《商用密码应用与安全性评估》第二章 商用密码应用与安全性评估政策法规-删减版小结
MySQL Samsung index and cost calculation
山东大学项目实训树莓派提升计划二期(三)SSH远程连接
Zabbix5系列-钉钉告警 (十五)
《商用密码应用与安全性评估》第一章 密码基础知识-小结
Zabbix5系列-监控mysql(5.7/5.8/mariadb) (十)
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (4)
Unittest单元测试(二)
Analysis of MySQL connection query cost and cost statistics