在用java Thread interrupt的时候为啥会抛出异常?
public class Demo1 extends Thread {
@Override
public void run(){
while (!interrupted()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " is executing");
}
}
public static void main(String[] args) {
Demo1 t1 = new Demo1();
Demo1 t2 = new Demo1();
t1.start();
t2.start();
t1.interrupt();
}
}
输出结果:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at t2.Demo1.run(Demo1.java:9)
Thread-0 is executing
Thread-1 is executing
Thread-0 is executing
Thread-1 is executing
Thread-0 is executing
Thread-1 is executing
Thread-0 is executing
Thread-1 is executing
Thread-0 is executing
Thread-1 is executing
Thread-0 is executing
Thread-1 is executing
.
.
.
为什么没有直接中断呢? 都是按照教程敲的,搞不明白。。。希望大神指点~