次のテスト コードを使用して InterruptedException をテストしています。
Runnable runMe = new Runnable() {
@Override
public void run() {
for(int i=0; i<6; i++) {
System.out.println("i = "+i);
if(i==3) {
System.out.println("i==3, Thread = "
+Thread.currentThread().getId());
//I invoke interrupt() on the working thread.
Thread.currentThread().interrupt();
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
//I caught InterruptedException
System.out.println(Thread.currentThread().getId()
+ " is interrupted!");
Thread.currentThread().interrupt();
}
}
}
}
};
Thread workingThread = new Thread(runMe);
workingThread.start();
try {
workingThread.join();
} catch (InterruptedException e) {
//Swallow
}
//the following log shows me workingThread.isInterrupted() is false, why?
System.out.println("workingThread("
+ workingThread.getId()+") interrupted ? "
+ workingThread.isInterrupted());
ではrun()
、interrupt()
現在作業中のスレッドで、InterruptedException
.
メインスレッドでは、コードの最後の行はSystem.out.println(...)
、作業スレッドの割り込みステータスを出力するものです。に引っかかっInterruptedException
たので、 trueというメッセージがrun()
表示されるはずなのに、false になったのはなぜですか?workingThread.isInterrupted()