1.thread.interrupt
中断可以理解为线程的一个标识位属性,它表示一个运行中的线程是否被其他线程进行了中断操作。
中断好比其他线程对该线程打了个招呼,其他线程通过调用该线程的interrupt()方法对其进行中断操作。
thread.interrupt会把isInterrupted设置成true,isInterrupted默认是false。
isInterrupted(中断状态)是线程的一个标识位,而中断操作是一种简便的线程间交互方式,而这种交互方式最适合用来取消或停止任务。
停止线程(thread.stop)由thread.interrupt替代,暂停(thread.suspend)和恢复(thread.resume)线程由等待/通知机制来替代。
2.thread.isInterrupted
线程通过检查自身是否被中断来进行响应,线程通过方法isInterrupted()来进行判断是否被中断。
如果该线程已经处于终结状态,即使该线程被中断过,在调用该线程对象的isInterrupted()时依旧会返回false。
3.Thread.interrupted:复位
线程也可以调用静态方法Thread.interrupted()对当前线程的中断标识位进行复位。
会把isInterrupted设置成false。
4.InterruptedException
声明抛出InterruptedException的方法(例如Thread.sleep(longmillis)方法)在抛出InterruptedException之前,
Java虚拟机会先将该线程的中断标识位清除,然后抛出InterruptedException,此时调用isInterrupted()方法将会返回false。