标签:print 线程 OLE ble pac new stat 监视 transport
package com.mozq.demo.demo;
class Task implements Runnable{
//public boolean flag = false;//存在可见性问题,主线程无法监视到线程执行成功。
public volatile boolean flag = false;
@Override
public void run() {
if(!flag){
System.out.println("执行任务");
}
flag = true;
System.out.println("任务执行完毕");
}
}
public class HeiMaVolatileTest {
public static void main(String[] args) {
Task task = new Task();
new Thread(task).start();
while (true){
if(task.flag){
System.out.println("=success=");
}
}
}
}
# 主线程获取的flag始终是false,不会打印success。
Connected to the target VM, address: '127.0.0.1:21030', transport: 'socket'
执行任务
任务执行完毕
标签:print 线程 OLE ble pac new stat 监视 transport
原文地址:https://www.cnblogs.com/mozq/p/12038732.html