首页 > 代码库 > Java并发程序设计(10)线程池之任务结果的异常处理

Java并发程序设计(10)线程池之任务结果的异常处理

 

1.1. 任务结果的异常处理

如果任务执行过程中发生异常,则get()方法会产生ExecutionException

 

ExecutorService  executorService = Executors.newFixedThreadPool(2);

Future<Integer>  future = executorService.submit( new Callable(){

 

@Override

public Integer  call() throws Exception {

throw new RuntimeException("some exception.");

//return 1;

}

 

 

});

 

try {

int  ret = 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)

 

 

如果任务执行过程中,线程被打断,则get()方法产生InterruptedException。

已知任务执行过程中sleep()/wait()时,如果shutdownNow()方法执行了,get会捕捉到sleep()产生的InterruptException,但是是通过ExecutionException的方式产生,而不是InterruptedException的方式。

 

ExecutorService  executorService = Executors.newFixedThreadPool(2);

Future<Integer>  future = executorService.submit( new Callable(){

 

@Override

public Integer  call() throws Exception {

Thread.sleep(1000*10);

System.out.println("------after sleep----------");

return 1;

}

 

 

});

 

executorService.shutdownNow();

 

 

try {

int  ret = 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)

 

 

如果调用get()方法的线程被打断,则会导致get()方法产生InterruptedException

ExecutorService  executorService = Executors.newFixedThreadPool(2);

final Thread  mainThread = Thread.currentThread();

Future<Integer>  future = executorService.submit( new Callable(){

 

@Override

public   Integer   call() throws Exception {

Thread.sleep(1000*3);

mainThread.interrupt();

return 1;

}

 

 

});

 

 

try {

int  ret = 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并发程序设计(10)线程池之任务结果的异常处理