interrupt when sleep
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(){
@Override
public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("interrupt");
break;
}
try {
Thread.sleep(1000);//breakpoint
} catch (InterruptedException e) {
System.out.println("interrupt when sleep");
Thread.currentThread().interrupt();
}
Thread.yield();
}
}
};
t1.start();
Thread.sleep(1000);//breakpoint
t1.interrupt();
}
}
debugger
![thread thread](http://static.javashuo.com/static/loading.gif)
重點:
1.線程睡眠(sleep)時,會丟失中斷(interrupt)標誌
2.中斷線程interrupt,只是對thread設置中斷標誌,並不會結束線程
3.interrupt不會釋放監視器(鎖)