標籤:over tac cat 輸出 executors 1.5 sys logs fixed
1.1. 線程池中的線程的異常終止
如果線程池中的線程的任務代碼發生異常導致線程終止,線程池會自動建立一個新線程。
對於各種類型的線程池,都是如此。以下代碼在單個線程的線程池中拋出一個異常,可以發現後續任務中輸出的每個tid的值都不相同。
ExecutorService executorService = Executors.newSingleThreadExecutor();for(int j=0;j<10;j++){final int t = j;executorService.execute( new Runnable(){@Overridepublic 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(){@Overridepublic 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)線程池之異常終止和正常關閉