Java并发编程(七)线程中断

逆流者 2021年02月21日 24次浏览

Java中的线程中断是一种线程间的协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是被中断的线程根据中断状态自行处理。

  • void interrupt()方法
    中断线程,例如,当线程A运行时,线程B可以调用线程A的interrupt()方法来设置线程A的中断标志为true并立即返回。设置标志仅仅是设置标志,线程A实际并没有被中断,它会继续往下执行。如果线程A因为调用了 wait系列函数、join方法或者slep方法而被阻塞挂起,这时候若线程B调用线程 A的interrupt()方法,线程A会在调用这些方法的地方抛出 InterruptedException异常而返回。
  • boolean isinterrupted() 方法
    检测当前线程是否被中断,如果是返回true,否则返回 false.
    源码中传递的false,说明不清除中断标志
public boolean isInterrupted() {
       return isInterrupted(false);
}
  • boolean interupted() 方法
    检测当前线程是否被中断,如果是返回true,否则返回 false。
    与isInterupted不同的是,该方法如果发现当前线程被中断,则会清除中断标志,并且该方法是static方法,可以通过Thread类直接调用。
    另外从源码可以知道,在interrupted()内部是获取当前调用线程的中断标志而不是调用 interrupted() 方法的实例对象的中断标志。
public static boolean interrupted() {
      return currentThread().isInterrupted(true);
   }

根据中断标志判断线程是否终止

public class InterruptTest {

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                // 如果当前线程被中断则退出循环
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread() + " hello");
                }
            }
        });

        threadA.start();

        Thread.sleep(1000);

        System.out.println("main thread interrupt child threadA");
        threadA.interrupt();

        threadA.join();
        System.out.println("main is over");

    }
}
....
Thread[Thread-0,5,main] hello
Thread[Thread-0,5,main] hello
main thread interrupt child threadA
Thread[Thread-0,5,main] hello

中断休眠中的线程以使睡眠中的线程恢复到激活状态

public class InterruptTest2 {

    public static void main(String[] args) throws InterruptedException {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println("threadA begin sleep for 2000s");
                    Thread.sleep(2000000);
                    System.out.println("threadA awaking");
                } catch (InterruptedException e) {
                    System.out.println("threadA is interrupted while sleeping");
                    return;
                }

                System.out.println("threadA-leaving normally");
            }
        });

        threadA.start();

        // 确保子线程进入休眠状态
        Thread.sleep(1000);

        // 打断子线程的休眠,让子线程从sleep函数返回
        threadA.interrupt();

        // 等待子线程执行完毕
        threadA.join();

        System.out.println("main thread is over");

    }
}
threadA begin sleep for 2000s
threadA is interrupted while sleeping
main thread is over

对比isInterrupted()和interrupted()

  • isInterrupted() 返回中断标志, 不会清除中断标志
  • interrupted() 返回中断标志, 会清除中断标志, 这个是指的当前线程

示例一

public class InterruptTest3 {

    public static void main(String[] args) throws InterruptedException {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {

                for (; ; ) {
                }

            }
        });

        threadA.start();

        // 设置中断标志
        threadA.interrupt();

        // 获取中断标志
        System.out.println("isInterrupted:" + threadA.isInterrupted());

        // 获取中断标志并重置
        System.out.println("isInterrupted:" + threadA.interrupted());

        // 获取中断表示并重置
        System.out.println("isInterrupted:" + Thread.interrupted());

        // 获取中断标志
        System.out.println("isInterrupted:" + threadA.isInterrupted());

        threadA.join();

        System.out.println("main thread is over");

    }
}
isInterrupted:true
isInterrupted:false
isInterrupted:false
isInterrupted:true

代码中threadA.interrupted()Thread.interrupted() 返回的是主线程的中断标志, 当前线程就是主线程.

示例二

public class InterruptTest4 {

    public static void main(String[] args) throws InterruptedException {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {

                // 中断标志为true时会退出循环,并且清除中断标志
                while (!Thread.interrupted()) {}

                System.out.println("threadA isInterrupted:"+Thread.currentThread().isInterrupted());

            }
        });

        threadA.start();

        // 设置中断标志
        threadA.interrupt();

        threadA.join();

        System.out.println("main thread is over");

    }
}

threadA isInterrupted:false
main thread is over

看执行结果, 可以看出子线程threadA被主线程中断之后退出了循环, 并且清除了自己的中断标志。