Interrupt相关方法
Interrupt方法
public void interrupt()
interrupt方法用来设置线程打断标志,但不会修改线程的运行状态
isInterrupted方法
public boolean isInterrupted()
实例方法,用来获取线程是否被设置打断标志
interrupted方法
public static boolean interrupted()
静态方法,用来获取进程打断标志,同时会清除打断标志
Interrupt与sleep/wait
线程sleep过程中如果被打断,会抛出异常 InterruptedException,sleep会提前结束,并且此时线程的打断标志会被清除。如果需要在被打断时做出响应,可以捕获异常后处理。
public class InterruptSleep {
public static void main(String[] args) {
Thread t = new Thread(() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {