我thread.isInterrupted
对下面程序中的行为有点困惑。
public class ThreadPractice {
public static void main(String args[]) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Starting thread..." + Thread.currentThread().getName());
Thread.sleep(10000);
System.out.println("Waking up");
}catch(InterruptedException e){
System.out.println("Thread is interrupted!!!");
Thread.currentThread().interrupt();
}
}
});
t.start();
Thread.sleep(2000);
t.interrupt();
//System.out.println(!t.isInterrupted());
while(!t.isInterrupted()){
System.out.println("Current thread not interrupted!!!");
}
}
}
当执行上述程序时,它会打印,
Starting thread...Thread-0
Thread is interrupted!!!
但是当我取消注释System.out
打印中断状态的语句时,它会进入一个无限循环打印“当前线程未中断”
我无法弄清楚究竟有什么不同的System.out
说法。