public class Server {
private static boolean msgForAlice=false;
private static boolean msgForBob=false;
public static void shouldAliceWait() {
System.out.println(msgForAlice);
System.out.println(msgForBob);
}
public static void main(String[] args) {
System.out.println("Before:");
System.out.println(msgForAlice);
System.out.println(msgForBob);
try {
Thread.sleep(2000);
}catch(Exception e) {
e.printStackTrace();
}
msgForBob=true;
System.out.println("After:");
System.out.println(msgForAlice);
System.out.println(msgForBob);
try {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
console.readLine();
}catch(Exception e) {
e.printStackTrace()
}}
public class Test {
public static void main(String[] args) {
Server.shouldAliceWait();
}
}
When main method of class Test is called while Server's main method is beeing blocked by readLine() call, I get unusual values from Server's static fields:
Server:
Before:
false
false
After:
false
true
Test:
false
false
Can someone explain me?