java中断线程执行interrupt()方法。这个方法不能中断正在运行的线程中。中断的唯一时刻是在任务进入到阻塞的操作中或者已经在阻塞操作内部时。调用该方法的线程的状态将被置为"中断"状态。注意:线程中断仅仅是设置线程的中断状态位,不会停止线程。所以当一个线程处于中断状态时,如果再由wait、sleep以及jion三个方法引起的阻塞,那么JVM会将线程的中断标志重新设置为false,并抛出一个InterruptedException异常。
public class Test2 {
public static void main(String[] args) {
for(int i =0;i<10;i++){
Thread.currentThread().interrupt();//修改标识 为true
System.out.println("after-"+Thread.currentThread().interrupted());//执行之后,将标识修改为false(清除标识位)
System.out.println("befor-"+Thread.currentThread().interrupted());
}
}
执行结果:
after:true
befor:false