51,411
社区成员
发帖
与我相关
我的任务
分享
import java.util.concurrent.TimeUnit;
public class PrintThread {
private static volatile boolean flag = false;
public static void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if(flag) {
System.out.println("Success");
break;
}
}
}
}).start();
TimeUnit.SECONDS.sleep(5);
System.out.println("设置为true");
flag = true;
}
}
正常输出:
----------
设置为true
Success