I have tested something about multithreading. I find the snippet lasts much more than 3 seconds, and is not printing the last System.out.println("program end");
. Why?
public class hello {
static Boolean flag = false;
public static void main(String args[]) throws InterruptedException {
MyThread t = new MyThread();
t.start();
Thread.sleep(3000);
hello.flag = true;
}
static class MyThread extends Thread {
public void run(){
System.out.println("Thread start");
while(true){
if(hello.flag){
break;
}
}
System.out.println("Thread end");//why not print this statement? but run in debug mode, it will print this statement correctly
}
}
}