Thread类的sleep(),wait()等方法,在接收到interrupt()方法中断时,会抛出异常,同时会将中断标志置为false,如果确实需要中断该线程,则应该在捕捉到异常后,继续调用interrupt()方法进行中断。
为什么不在异常时直接中断线程呢?主要是为了防止线程的资源没有得到释放而中断了线程
public class UserThread extends Thread(){
public void run(){
while(!isInterrupt()){
try{
Thread.sleep(1000);
}catch(InterruptedException ex){
System.out.println(Thread.currentThread().getName+" Exception ,interrupt flag is "+isInterrupted());
//释放资源
doRelease();
interrupt();
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName+" runing");
}
System.out.println(Thread.currentThread().getName+" interrupt flag is "+isInterrupted());
}
public static void main(String args){
UserThread thread = new UserThread();
thread.start();
Thread.sleep(30);
thread.interrupt();
}
}