博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThreadPoolExecutor异常处理
阅读量:7013 次
发布时间:2019-06-28

本文共 1984 字,大约阅读时间需要 6 分钟。

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. }  

 

转载地址:http://euqtl.baihongyu.com/

你可能感兴趣的文章
linux下的开源移动图像监测程序--motion编译与配置【转】
查看>>
git的版本回退探索
查看>>
记一次docker问题定位(perf,iostat等性能分析)
查看>>
H3c 配置ssh acl
查看>>
【IntellJ IDEA】idea上 实现了Serializable接口,要自动生成serialVersionUID的方法
查看>>
帮助函数
查看>>
自己动手写Android插件化框架,让老板对你刮目相看
查看>>
[转]Object.keys()和for in的排序问题
查看>>
Dedecms判断当前栏目下是否有子栏目
查看>>
常见的加密和解密算法—AES
查看>>
HTTPS那些事(二)SSL证书(转载)
查看>>
【PIC学习第2例】PIC16F877A LED闪烁
查看>>
Orchard CMS前台页面为什么没有Edit链接?
查看>>
判断URL文件是不是在于在。
查看>>
GNU C中的零长度数组(转载)
查看>>
SQL2005中时,Diagrams的问题
查看>>
[转]软件架构的一致性
查看>>
拨云见日,任重道远 ——第六届云计算大会感悟
查看>>
PID控制器的应用:控制网络爬虫抓取速度
查看>>
cad中关于点样式点的绘制
查看>>