Ai
1 Star 0 Fork 59

Yangjianwei/java-performance

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CPUCacheTest.java 791 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiandafu 提交于 2019-11-12 14:15 +08:00 . ”test“
package com.ibeetl.code.ch03;
/**
* 一个经典的多线程编程陷阱,由于stop可能从cpu缓存里去读,主线程通过设置stop并不能停止线程B
* 可以使用volatile保证可见性,也可以试试14行,15行的代码,也能起到"神奇"效果
* @author 公众号 java系统优化
*/
public class CPUCacheTest {
private static /* volatile */ boolean stop = false;
public static void main(String[] args){
Thread a = new Thread("B"){
public void run(){
while (!stop) {
// pause(1);
// System.out.println(1);
int a = 1;
}
System.out.println("exit");
}
};
a.start();
pause(100);
stop = true;
}
public static void pause(int time){
try {
Thread.sleep(time);
}catch(Exception ex){
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/higher/java-performance.git
git@gitee.com:higher/java-performance.git
higher
java-performance
java-performance
master

搜索帮助