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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)线程池之任务结果的异常处理的主要内容,如果未能解决你的问题,请参考以下文章

Java并发程序设计线程池之获取任务执行结果

JAVA 线程池之Callable返回结果

Java并发程序设计线程池之用于定时器

Java并发程序设计线程池之线程数量的控制

Java线程池之ThreadPoolExecutor

JAVA线程池之周期性地执行任务