Java Concurrency Programming (7) thread pool abnormal termination and normal shutdown

Source: Internet
Author: User

1.1.unexpected termination of thread in thread pool

If a thread in the thread pool has an exception to the task code that causes the thread to terminate, a new thread is automatically created by the thread pooling.

This is true for various types of thread pools. The following code throws an exception in the thread pool of a single thread, and you can see that the values for each tid that are output in subsequent tasks are not the same.

Executorservice Executorservice =executors.newsinglethreadexecutor (); for(intj=0;j<10;j++){Final intt =J;executorservice.execute (NewRunnable () {@Override Public voidrun () { for(inti=0;i<4;i++) {System.out.println ("T:" + t + "," + "I:" + i + ", Tid:" +Thread.CurrentThread (). GetId ());intA = 3/0;//the resulting/by zero exception, thread termination. The new task creates a new thread. }}});} 

The output information can be seen for each task (t) , the thread ID (TID) are different.

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.termination of the thread pool

Both Shutdownnow () and shutdown () can terminate the thread pool, both terminating the thread pool and blocking the submission of new tasks.

Shutdownnow () stops the currently executing task, clears the committed but not yet running task, and then terminates the thread pool.

Shutdown () waits for all tasks that have already been committed to complete before terminating the thread pool.

Executorservice Executorservice = Executors.newfixedthreadpool (2); for(intj=0;j<10;j++){Final intt =J;executorservice.execute (NewRunnable () {@Override Public voidrun () { for(inti=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 Concurrency Programming (7) thread pool abnormal termination and normal shutdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.