Java concurrency programming (10) exception handling of task result of thread pool

Source: Internet
Author: User

1.1. exception handling for task results

If an exception occurs during the execution of a task, the get () method produces executionexception.

Executorservice Executorservice = Executors.newfixedthreadpool (2); future<Integer> future = Executorservice.submit (Newcallable () {@Override publicInteger Call ()throwsException {Throw NewRuntimeException ("some exception.");//return 1;}  }); Try {intRET =Future.get (); System.out.println ("return:" +ret);} Catch(interruptedexception e) {e.printstacktrace ();}Catch(executionexception e) {e.printstacktrace ();}

java.util.concurrent.ExecutionException: java.lang.RuntimeException: some Exception.

At Java.util.concurrent.FutureTask.report (Unknown Source)

At Java.util.concurrent.FutureTask.get (Unknown Source)

At Com.test.concurrence.ThreadPoolTest.testFutureThreadPool (threadpooltest.java:34)

At Com.test.concurrence.ThreadPoolTest.main (threadpooltest.java:16)

caused by: java.lang.RuntimeException: some Exception.

At Com.test.concurrence.threadpooltest$1.call (threadpooltest.java:26)

At Com.test.concurrence.threadpooltest$1.call (threadpooltest.java:1)

At Java.util.concurrent.FutureTask.run (Unknown Source)

At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source)

At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source)

At Java.lang.Thread.run (Unknown Source)

if the thread is interrupted during the execution of the task, the Get () method produces Interruptedexception.

when sleep ()/wait () is known during the execution of a task , if the shutdownnow () method executes,get snaps to Sleep () produced by interruptexception , but is through executionexception the way to produce rather than interruptedexception the Way.

Executorservice Executorservice = Executors.newfixedthreadpool (2); future<Integer> future = Executorservice.submit (Newcallable () {@Override publicInteger Call ()throwsException {thread.sleep (1000*10); System.out.println ("------after sleep----------");return1;} });  Executorservice.shutdownnow (); Try {intRET =Future.get (); System.out.println ("return:" +ret);} Catch(interruptedexception e) {System.out.println ("---------interrupted exception--------"); E.printstacktrace ();} Catch(executionexception e) {System.out.println ("---------execution exception--------"); E.printstacktrace ();} Catch(cancellationexception e) {System.out.println ("---------cancellation exception--------"); E.printstacktrace (); }

---------execution exception--------

java.util.concurrent.ExecutionException: java.lang.InterruptedException: Sleep Interrupted

At Java.util.concurrent.FutureTask.report (Unknown Source)

At Java.util.concurrent.FutureTask.get (Unknown Source)

At Com.test.concurrence.ThreadPoolTest.testFutureThreadPool (threadpooltest.java:39)

At Com.test.concurrence.ThreadPoolTest.main (threadpooltest.java:17)

caused by: java.lang.InterruptedException: Sleep Interrupted

At Java.lang.Thread.sleep (Native Method)

At Com.test.concurrence.threadpooltest$1.call (threadpooltest.java:27)

At Com.test.concurrence.threadpooltest$1.call (threadpooltest.java:1)

At Java.util.concurrent.FutureTask.run (Unknown Source)

At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source)

At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source)

At Java.lang.Thread.run (Unknown Source)

if the thread that calls the Get () method is interrupted, it causes the Get () method to produce interruptedexception.

Executorservice Executorservice = Executors.newfixedthreadpool (2);FinalThread Mainthread =Thread.CurrentThread (); future<Integer> future = Executorservice.submit (Newcallable () {@Override publicInteger Call ()throwsException {thread.sleep (1000*3); mainthread.interrupt ();return1;}  }); Try {intRET =Future.get (); System.out.println ("return:" +ret);} Catch(interruptedexception e) {System.out.println ("---------interrupted exception--------"); E.printstacktrace ();} Catch(executionexception e) {System.out.println ("---------execution exception--------"); E.printstacktrace ();} Catch(cancellationexception e) {System.out.println ("---------cancellation exception--------"); E.printstacktrace (); } 

---------interrupted exception--------

Java.lang.InterruptedException

At Java.util.concurrent.FutureTask.awaitDone (Unknown Source)

At Java.util.concurrent.FutureTask.get (Unknown Source)

At Com.test.concurrence.ThreadPoolTest.testFutureThreadPool (threadpooltest.java:38)

At Com.test.concurrence.ThreadPoolTest.main (threadpooltest.java:17)

Java concurrency programming (10) exception handling of task result of thread pool

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.