Java 线程中断

1,337 阅读2分钟

Java如何实现线程中断?

通过调用Thread类的实例方法interrupt。如下:

Thread thread = new Thread(){
            @Override
            public void run() {
                if(isInterrupted()){
                    System.out.println("interrupt");
                }
            }
        };
        thread.start();

        thread.interrupt();

线程中断后线程会立即停止执行吗?

NO。 而如果线程未阻塞,或未关心中断状态,则线程会正常执行,不会被打断。

Thread.interrupt()的官方解释是这样的:

If this thread is blocked in an invocation of the Object#wait() wait(), { Object#wait(long) wait(long)}, or { Object#wait(long, int) wait(long, int)} methods of the { Object} class, or of the { #join()}, { #join(long)}, { #join(long, int)}, { #sleep(long)}, or { #sleep(long, int)}, methods of this class, then its interrupt status will be cleared and it will receive an { InterruptedException}.

也就是:处于阻塞的线程,即在执行Object对象的wait()、wait(long)、wait(long, int),或者线程类的join()、join(long)、join(long, int)、sleep(long)、sleep(long,int)方法后线程的状态,当线程调用interrupt()方法后,这些方法将抛出InterruptedException异常,并清空线程的中断状态。

比如下面的例子会中断两次,第一次sleep方法收到中断信号后抛出了InterruptedException,捕获异常后中断状态清空,然后继续执行下一次:

public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("interrupt");

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();

        thread.interrupt();
        Thread.sleep(5000);
        thread.interrupt();
    }

而下面这个例子则会一直执行,不会被打断:

public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                while (true)
                System.out.println("interrupt");
            }
        };
        thread.start();

        thread.interrupt();
    }

interrupted与isInterrupted方法啥区别?

  • Thread类的静态方法interrupted:测试当前线程是否已经中断。如果线程处于中断状态返回true,否则返回false。同时该方法将清除的线程的中断状态。即:如果连续两次调用该方法,则第二次调用将返回false。该方法可用于清除线程中断状态使用。
  • Thread类的实例方法isInterrupted:测试线程是否已经中断。线程的中断状态不受该方法的影响。

Thread类并没有提供单独清除中断状态的方法,所以有两种方式来达到此目的:

  • 对于sleep等阻塞方法,catch InterruptedException异常;
  • 调用Thread类的静态方法interrupted

线程中断有哪些实际应用?

线程中断的几个实际应用场景:

  • 在处理Web请求时,可能将请求分配到多个线程去处理,实现请求执行的超时机制;
  • 实现线程池时,关闭线程池中的线程任务。