先上代码,第一种情况,想在多线程运行的过程中,打出当前的线程名称,代码如下:
public class VolatileVisibilityTest {
private static boolean initFlag = false;
public static void main(String[] args) throws InterruptedException {
System.out.println("1 " + Thread.currentThread().getName());
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("等待数据准备。。");
while (!initFlag) {
System.out.println("2 " + Thread.currentThread().getName());
}
System.out.println("==================数据准备完毕,执行程序逻辑");
}
}).start();
Thread.sleep(100);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("3