线程中断

一个线程执行完毕之后会自动结束,如果在运行过程中发生异常也会提前结束。

InterruptedException

通过调用一个线程的 interrupt() 来中断该线程,如果该线程处于阻塞、限期等待或者无限期等待状态,那么就会抛出 InterruptedException,从而提前结束该线程。但是不能中断 I/O 阻塞和 synchronized 锁阻塞。

对于以下代码,在 main() 中启动一个线程之后再中断它,由于线程中调用了 Thread.sleep() 方法,因此会抛出一个 InterruptedException,从而提前结束线程,不执行之后的语句。

 1 public class InterruptExample {
 2 
 3     private static class MyThread1 extends Thread {
 4         @Override
 5         public void run() {
 6             try {
 7                 Thread.sleep(2000);
 8                 System.out.println("Thread run");
 9             } catch (InterruptedException e) {
10                 e.printStackTrace();
11             }
12         }
13     }
14 }
15 public static void main(String[] args) throws InterruptedException {
16     Thread thread1 = new MyThread1();
17     thread1.start();
18     thread1.interrupt();
19     System.out.println("Main run");
20 }
View Code

 

Main run
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at InterruptExample.lambda$main$0(InterruptExample.java:5)
    at InterruptExample$$Lambda$1/713338599.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
View Code

 

interrupted()

如果一个线程的 run() 方法执行一个无限循环,并且没有执行 sleep() 等会抛出 InterruptedException 的操作,那么调用线程的 interrupt() 方法就无法使线程提前结束。

但是调用 interrupt() 方法会设置线程的中断标记,此时调用 interrupted() 方法会返回 true。因此可以在循环体中使用 interrupted() 方法来判断线程是否处于中断状态,从而提前结束线程。

 1 public class InterruptExample {
 2 
 3     private static class MyThread2 extends Thread {
 4         @Override
 5         public void run() {
 6             while (!interrupted()) {
 7                 // ..
 8             }
 9             System.out.println("Thread end");
10         }
11     }
12 }
13 public static void main(String[] args) throws InterruptedException {
14     Thread thread2 = new MyThread2();
15     thread2.start();
16     thread2.interrupt();
17 }
View Code

 

Thread end

Executor 的中断操作

调用 Executor 的 shutdown() 方法会等待线程都执行完毕之后再关闭,但是如果调用的是 shutdownNow() 方法,则相当于调用每个线程的 interrupt() 方法。

以下使用 Lambda 创建线程,相当于创建了一个匿名内部线程。

 1 public static void main(String[] args) {
 2     ExecutorService executorService = Executors.newCachedThreadPool();
 3     executorService.execute(() -> {
 4         try {
 5             Thread.sleep(2000);
 6             System.out.println("Thread run");
 7         } catch (InterruptedException e) {
 8             e.printStackTrace();
 9         }
10     });
11     executorService.shutdownNow();
12     System.out.println("Main run");
13 }
View Code

 

Main run
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at ExecutorInterruptExample.lambda$main$0(ExecutorInterruptExample.java:9)
    at ExecutorInterruptExample$$Lambda$1/1160460865.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
View Code

 

如果只想中断 Executor 中的一个线程,可以通过使用 submit() 方法来提交一个线程,它会返回一个 Future<?> 对象,通过调用该对象的 cancel(true) 方法就可以中断线程。

1 Future<?> future = executorService.submit(() -> {
2     // ..
3 });
4 future.cancel(true);

 

posted @ 2019-07-06 19:43  惯看秋风  阅读(16)  评论(0)    收藏  举报