【发布时间】:2015-02-13 22:09:11
【问题描述】:
我对下面程序中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 语句的确切区别。
【问题讨论】:
标签: java multithreading interrupted-exception