public class InterruptTest extends Thread
{
public void run()
{
try {
Thread.sleep(1000);
System.out.println("Finished!");
}
catch (InterruptedException e) {
System.out.println("I am interrupted!");
System.out.println(Thread.interrupted());// showing the result after exception throw as well as flag set to false
System.exit(-1);
}
}
public static void main(String args[])
{
InterruptTest t1 = new InterruptTest();
Thread t= new Thread(t1);
t.start();
System.out.println(t.isInterrupted());// ask for the judgment in first time before calling interrupt().
t.interrupt();
System.out.println(t.isInterrupted());// ask for the judgment in second time after calling interrupt().
System.out.println(t.isInterrupted());// ask for the judgment in third time after calling interrupt(). Very Puzzled!!!
System.out.println(t.isInterrupted());// ask for the judgment in fourth time after calling interrupt().
}
}
Console Display:
false// sure
true// not sure
true// not sure
true// not sure
I am interrupted!// sure
false// sure
false// sure
true// not sure
true// not sure
false// not sure
I am interrupted!// sure
false// sure
false// sure
true// not sure
false// not sure
false// not sure
I am interrupted!// sure
false// sure
コードは、Javaスレッドとinterrupt()メソッドに関係しています。同じコードを何度も実行することで予期しない結果がどうなるか教えてください。なぜこれらの結果は一意ではありませんか? .................................................. ..................................皆さん、ありがとうございました!Javaスレッドと割り込み、強力なパズル
あなたが私のパズルについて明確でない場合は、気軽に! ☺ –