In this bit of code, there are 2 functionally identical while loops, but the first one prints "GO!" and the second one hangs forever. What is going on?
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;
}
}
}