中断

1、当中断异常被捕获时,中断标识自动清除

2、中断标识可以当成一个标识位,看成一个volatile变量。经常被用来当做通知线程结束或者唤醒线程的标识。

3、Thread.interrupted()也能自动的清除中断位

package com.ydd.study.hello.thread;

public class Interrupt {
public static void main(String[]a){
    Thread t=new Thread(){
        @Override
        public void run(){
            for(int i=0;i<100;i++){
                System.out.println(i);
            }

            synchronized(this){
                try {
                    System.out.println(Thread.currentThread().isInterrupted());
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
    t.interrupt();
}
}

线程的中断标识是true,然后调用wait的时候,立刻抛异常。Object的wait,notify等全是native方法。

原文地址:https://www.cnblogs.com/YDDMAX/p/5635928.html