java - 多线程循环仅适用于添加的空 print("") 语句
问题描述
在这段代码中,有 2 个功能相同的 while 循环,但第一个输出的是“GO!” 第二个永远挂起。到底是怎么回事?
public class Tests {
public static boolean go=false;
public static void main(String[] args) {
new TimerThread().start();
// Loop 1 - Prints "GO!"
while(true){
System.out.print("");
if(go){
System.out.println("GO!");
break;
}
}
go=false;
new TimerThread().start();
// Loop 2 - Hangs
while(true){
if(go){
System.out.println("GO!");
break;
}
}
}
static class TimerThread extends Thread{
public void run(){
double timer = System.currentTimeMillis();
while(timer>System.currentTimeMillis()-3000){
go=false;
}
go=true;
}
}
}
解决方案
推荐阅读
- solaris - 将 ZFS 池开放为可写
- java - 如果启动了较新的实例,则终止程序的较旧实例
- sql - 在 Hive 中的多个列上展开
- c++ - TCP C在没有收到数据时发送数据
- glassfish - 当我的 SSL 证书在 Payara 5 中更新和替换时,为什么我会收到 CertificationExpiredException?
- sql - 包含一列以计算具有特定值的记录
- c# - 当我将绘制模式更改为 OwnerDrawFixed TabControl 时,将外观更改为 3D
- ios - 如何在 Swift 中组合正则表达式?
- java - Sonar Spring JDBC queryForObject 不能返回 null
- raspberry-pi - NOC电视需要自动登录监控网站