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