首页 > 代码库 > Java并发程序设计(7)线程池之异常终止和正常关闭

Java并发程序设计(7)线程池之异常终止和正常关闭

1.1. 线程池中的线程的异常终止

如果线程池中的线程的任务代码发生异常导致线程终止,线程池会自动创建一个新线程。

对于各种类型的线程池,都是如此。以下代码在单个线程的线程池中抛出一个异常,可以发现后续任务中输出的每个tid的值都不相同。

 

 

ExecutorService  executorService = Executors.newSingleThreadExecutor();

for(int j=0;j<10;j++){

final int t = j;

executorService.execute( new Runnable(){

@Override

public void run() {

for(int i=0;i<4;i++){

System.out.println("t:" + t + "," + "i:" + i + ", tid:" + Thread.currentThread().getId());

int a = 3 /0;//产生/ by zero异常,线程终止。新任务将创建新线程。

}

}

});

}

 

 

输出信息中可以看到对每个任务(t),线程id(tid)都不同。

t:0,i:0, tid:8

t:1,i:0, tid:10

Exception in thread "pool-1-thread-1" t:2,i:0, tid:11

Exception in thread "pool-1-thread-2" Exception in thread "pool-1-thread-3" java.lang.ArithmeticException: / by zero

t:3,i:0, tid:12

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "pool-1-thread-4" java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

t:4,i:0, tid:13

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

t:5,i:0, tid:14

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

t:6,i:0, tid:15

Exception in thread "pool-1-thread-6" Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-7" java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

t:7,i:0, tid:16

at java.lang.Thread.run(Unknown Source)

java.lang.ArithmeticException: / by zero

t:8,i:0, tid:17 at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

 

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "pool-1-thread-9" Exception in thread "pool-1-thread-8" java.lang.ArithmeticException: / by zero

t:9,i:0, tid:18 at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

 

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "pool-1-thread-10" java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.ArithmeticException: / by zero

at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

 

 

1.2. 线程池的终止

shutdownNow()shutdown()都可以终止线程池,两者都可以终止线程池,阻止新任务的提交。

shutdownNow()会停止当前正在执行的任务,清除已提交但是尚未运行的任务,然后终止线程池。

shutdown()会等待已经提交的所有任务执行完毕才终止线程池。

ExecutorService  executorService = Executors.newFixedThreadPool(2);

for(int j=0;j<10;j++){

final int t = j;

executorService.execute( new Runnable(){

@Override

public void run() {

for(int i=0;i<4;i++){

String s = "t:" + t + "," + "i:" + i + ", tid:" + Thread.currentThread().getId();

System.out.println(s);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

 

});

 

}

 

executorService.shutdownNow();

 

 

 

 

t:0,i:0, tid:8

end.

t:1,i:0, tid:9

java.lang.InterruptedException: sleep interrupted

t:0,i:1, tid:8

at java.lang.Thread.sleep(Native Method)

at com.test.concurrence.ThreadPoolTest$1.run(ThreadPoolTest.java:27)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.InterruptedException: sleep interrupted

at java.lang.Thread.sleep(Native Method)

at com.test.concurrence.ThreadPoolTest$1.run(ThreadPoolTest.java:27)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

t:1,i:1, tid:9

t:0,i:2, tid:8

t:1,i:2, tid:9

t:0,i:3, tid:8

t:1,i:3, tid:9

 

Java并发程序设计(7)线程池之异常终止和正常关闭