import java.util.concurrent.CountDownLatch;
/**
* 代码重排示例
* 单例模式的DCL,当对象被分配空间后并且将内存的地址返回给对象后,其它线程再对实例做非空判断时已经不成立
*/
public class Main {
private static int x = 0, y = 0;
private static int a = 0, b = 0;
public static void main(String[] args) throws InterruptedException {
for (long i = 0; i < Long.MAX_VALUE; i++) {
x = 0;
y = 0;
a = 0;
b = 0;
CountDownLatch latch = new CountDownLatch(2);
Thread thread = new Thread(() -> {
a = 1;
x = b;
latch.countDown();
});
Thread thread1 = new Thread(() -> {
b = 1;
y = a;
latch.countDown();
});
thread.start();
thread1.start();
latch.await();
if (x == 0 && y == 0) {
String result = "第" + i + "次";
System.out.printf(result);
break;
}
}
}
}
java下代码重排所可能产生的问题
需积分: 1 32 浏览量
2024-03-27
11:54:06
上传
评论
收藏 2KB ZIP 举报
机智的程序员zero
- 粉丝: 1953
- 资源: 4202
最新资源
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
- WordPress后台美化插件QuarterAdmin分享
- PCB_Project单片机绘制 (2024-5-11 22-22-13).zip
- nccl-local-repo-ubuntu2204-2.21.5-cuda12.4-1.0-1-amd64
- STM32移植LVGL源码工程 stm32移植GUI-Guider源码 LVGL移植源码
- Screenshot_20240522_084328_com.tencent.mm.jpg
- 附件计算机专业课选课说明-1.xlsx
- 基于TypeScript的ahousepet-admin-web管理系统设计源码
- 《广东开放大学学习指引》期末考核要求0522.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈