<1>方法:public void interrupt(); //中断线程,实例方法
public boolean isInterrupted(); //判断是否被中断吗,实例方法
public static boolean Thread.interrupted(); //判断是否被中断,并清除当前状态
sleep()时,由于中断抛出了异常,会清除中断标志位,需要在异常处理中再次设置中断标志位
Thread t1=new Thread(){
@Override
public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("interrupted");
break;
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
System.out.println("interrupt when sleep");
Thread.currentThread().interrupt();
}
}
}
};
t1.start();
Thread.sleep(3000);
t1.interrupt();