码迷,mamicode.com
首页 > 其他好文 > 详细

ThreadPoolExecutor异常处理

时间:2017-07-26 00:19:45      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:shu   .com   返回   ++i   包装   tun   throwable   语言   wan   

java.util.concurrent包中的ThreadPoolExecutor,提供了java语言的线程池,你可以提交一个返回结果的任务(submit(Callable),返回Future),或者执行一个不返回结果的任务(execute(Runnable)),但提交的任务可能会抛异常,这就需要处理异常:

1. 对于submit的任务,框架会将异常保持在future里,并包装在ExecutionException里,当调用Future.get()时,再次throw,这时可以调用ExecutionException.getCause()获取包装的exception,这种情况下,设置UncaughtExceptionHandler也不会被调用。

2. 对应execute的任务,会直接throw,可以设置一个UncaughtExceptionHandler,例如:

Java代码  技术分享
  1. import java.lang.Thread.UncaughtExceptionHandler;  
  2. import java.util.Random;  
  3. import java.util.concurrent.CancellationException;  
  4. import java.util.concurrent.ExecutionException;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Future;  
  7. import java.util.concurrent.LinkedBlockingQueue;  
  8. import java.util.concurrent.ThreadFactory;  
  9. import java.util.concurrent.ThreadPoolExecutor;  
  10. import java.util.concurrent.TimeUnit;  
  11. import java.util.concurrent.atomic.AtomicInteger;  
  12.   
  13. public class ExecutorServiceDemo {  
  14.   
  15.     public static void testExecute() {  
  16.         ExecutorService executors = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS,  
  17.                 new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {  
  18.                     final AtomicInteger threadNumber = new AtomicInteger(1);  
  19.   
  20.                     public Thread newThread(Runnable r) {  
  21.                         Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "topPatternTasklet-thread"  
  22.                                 + (threadNumber.getAndIncrement()));  
  23.                         t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {  
  24.   
  25.                             public void uncaughtException(Thread t, Throwable e) {  
  26.                                 System.out.println(e);  
  27.                             }  
  28.                               
  29.                         });  
  30.                         return t;  
  31.                     }  
  32.                 }, new ThreadPoolExecutor.CallerRunsPolicy());  
  33.   
  34.         final Random r = new Random();  
  35.         for (int i = 0; i < 10; ++i) {  
  36.             executors.execute(new Runnable() {  
  37.   
  38.                 public void run() {  
  39.                     try {  
  40.                         int ri = r.nextInt(1000);  
  41.                         Thread.sleep(ri);  
  42.                         if (ri % 3 == 0) {  
  43.                             System.out.println("ri:" + ri);  
  44.                             throw new RuntimeException("haha error!");  
  45.                         }  
  46.                         System.out.println(Thread.currentThread());  
  47.                     } catch (InterruptedException e) {  
  48.                     }  
  49.                 }  
  50.             });  
  51.         }  
  52.   
  53.         executors.shutdown();  
  54.         System.out.println("finished");   
  55.     }  
  56.     public static void main(String[] args) {  
  57.         testExecute();  
  58.     }  
  59. }  

 

ThreadPoolExecutor异常处理

标签:shu   .com   返回   ++i   包装   tun   throwable   语言   wan   

原文地址:http://www.cnblogs.com/exmyth/p/7236977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!