当前位置:网站首页>What's the use of volatile
What's the use of volatile
2022-07-23 08:19:00 【LLAiden】
First, a small program
public class Main {
/*volatile */static boolean running = true;
public static void main(String[] args) throws InterruptedException {
m();
Thread.sleep(1000);
running = false;
System.out.println("set running = false");
Thread.sleep(1000);
System.out.println(running);
}
public static void m() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("start...");
while (running) {
}
System.out.println("end...");
}
}).start();
}
}This is a very simple small program , stay m Method if running by true It's a dead cycle , But sleep in the main thread 1 In seconds, the running Set to false Make it jump out of cycle printing end... Next, let's run the program and have a look
D:\dev\openjdk-18.0.2\bin\java.exe -javaagent:D:\dev\ideaIC-2022.1.4.win\lib\idea_rt.jar=60387:D:\dev\ideaIC-2022.1.4.win\bin -Dfile.encoding=UTF-8 -classpath D:\dev\java-code\JavaObjectLayout\JavaObjectLayout\target\classes;C:\Users\Administrator\.m2\repository\org\openjdk\jol\jol-core\0.16\jol-core-0.16.jar org.example.Main
start...
set running = false
falseFrom the running results, it doesn't jump out whlie Yeah, but running It is indeed set to false 了 , What we're going to do is running Variable plus volatile Modify and then see the operation effect
D:\dev\openjdk-18.0.2\bin\java.exe -javaagent:D:\dev\ideaIC-2022.1.4.win\lib\idea_rt.jar=60397:D:\dev\ideaIC-2022.1.4.win\bin -Dfile.encoding=UTF-8 -classpath D:\dev\java-code\JavaObjectLayout\JavaObjectLayout\target\classes;C:\Users\Administrator\.m2\repository\org\openjdk\jol\jol-core\0.16\jol-core-0.16.jar org.example.Main
start...
set running = false
end...
falseOn the plus volatile after while Just jump out of the cycle normally, because if you don't add volatile Words running It is invisible between threads , Every time a thread is used running It is the second thread that takes value directly from the register to modify running The first thread doesn't know the value of, which causes it to get the value directly from the register, and the value in the register is always true It will not jump out of the loop
volatile The function of is to tell the register that the value in your cache line may be incorrect , You need to get values from the main memory ,volatile Another function is to prohibit instruction reordering , When the two instructions are not related to each other cpu Instructions may be reordered, causing problems in multithreading, so we need to use variables shared by multithreads volatile modification
边栏推荐
- Can the formatted data of the USB flash disk be recovered? How to recover the formatted data of the USB flash disk
- Several ways of QT thread exit
- 如何高效安装MindSpore的GPU版本
- H7-TOOL串口脱机烧录操作说明,支持TTL串口,RS232和RS485(2022-06-30)
- 微信小程序中使用全局数据实现数据共享
- 深度解析kube-scheduler调度上下文
- Jmeter分布式压测
- 三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略
- 1.11 ArrayList & student management system
- 半定制数字反相器版图绘制
猜你喜欢

php可不可以拆分数组

Live broadcast preview | live broadcast Seminar on open source security governance models and tools

I can't be angry with "voluntary salary reduction". I'm naked. I'm three times in four days. How can it end like this?

又发福利!日历小程序源码

promise(二)

来,滑动到下一个小姐姐

直播预告 | 开源安全治理模型和工具直播研讨会

Wechat applet project practice

Redis profile

H7-TOOL串口脱机烧录操作说明,支持TTL串口,RS232和RS485(2022-06-30)
随机推荐
php数组下标是不是只能从0开始
synchronized是如何实现的
Has the live broadcast function of the multi merchant system been used? 666 for used friends!
Go 并发编程基础:什么是上下文
Jedis操作Redis
Typora设置标题自动添加序号
为什么有的人把代码写的如此复杂?
方正中期是什么级别的期货公司?开户安全可靠吗?
Typora set the title to automatically add sequence number
Leetcode day 26
WPS data splitting
什么时候使用UserCF,什么时候使用ItemCF?
传统银行票据打印系统几个关键技术点简要分析
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
挖财和启牛都是干什么的开户安全吗?
Redis中的数据类型
How to implement WebService service on the c/s side of C #
阿里云国际版忘记会员名或登录密码,怎么办?
Redis事务与锁机制
EMQX v4.4.5 发布:新增排他订阅及 MQTT 5.0 发布属性支持