Code:
public class Test {
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
start();
}
}).start();
Thread.sleep(100);
stopRunning = true;
System.out.println("But it didn't end?");
}
private static boolean stopRunning = false;
public static void start() {
while (!stopRunning) {
System.out.print("");
}
System.out.println("Method ended.");
}
}
"But it didn't end?
Method ended."
Code:
public class Test2 {
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
start();
}
}).start();
Thread.sleep(100);
stopRunning = true;
System.out.println("But it didn't end?");
}
private static boolean stopRunning = false;
public static void start() {
while (!stopRunning) {
int i = 0;
i++;
}
System.out.println("Method ended.");
}
}
"But it didn't end?"
Having a print out causes it to run (incorrect order though?), having anything else doesn't however