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,例如:

  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. public class ExecutorServiceDemo {
  13. public static void testExecute() {
  14. ExecutorService executors = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS,
  15. new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
  16. final AtomicInteger threadNumber = new AtomicInteger(1);
  17. public Thread newThread(Runnable r) {
  18. Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "topPatternTasklet-thread"
  19. + (threadNumber.getAndIncrement()));
  20. t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  21. public void uncaughtException(Thread t, Throwable e) {
  22. System.out.println(e);
  23. }
  24. });
  25. return t;
  26. }
  27. }, new ThreadPoolExecutor.CallerRunsPolicy());
  28. final Random r = new Random();
  29. for (int i = 0; i < 10; ++i) {
  30. executors.execute(new Runnable() {
  31. public void run() {
  32. try {
  33. int ri = r.nextInt(1000);
  34. Thread.sleep(ri);
  35. if (ri % 3 == 0) {
  36. System.out.println("ri:" + ri);
  37. throw new RuntimeException("haha error!");
  38. }
  39. System.out.println(Thread.currentThread());
  40. } catch (InterruptedException e) {
  41. }
  42. }
  43. });
  44. }
  45. executors.shutdown();
  46. System.out.println("finished");
  47. }
  48. public static void main(String[] args) {
  49. testExecute();
  50. }
  51. }

ThreadPoolExecutor异常处理的更多相关文章

  1. ThreadPoolExecutor

    ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...

  2. android线程池ThreadPoolExecutor的理解

    android线程池ThreadPoolExecutor的理解 线程池 我自己理解看来.线程池顾名思义就是一个容器的意思,容纳的就是ThreadorRunable, 注意:每一个线程都是需要CPU分配 ...

  3. Core Java 谈谈 ThreadPoolExecutor

    说起Java 7的Executors框架的线程池,同学们能想到有几种线程池,它们分别是什么? 一共有四个,它们分别是Executors的 newSingleThreadPool(), newCache ...

  4. 自定义 ThreadPoolExecutor 处理线程运行时异常

    自定义 ThreadPoolExecutor 处理线程运行时异常 最近看完了ElasticSearch线程池模块的源码,感触颇深,然后也自不量力地借鉴ES的 EsThreadPoolExecutor ...

  5. ThreadPoolExcutor 线程池 异常处理 (下篇)

    前言 因为这是之前面试的一个题目,所以印象比较深刻,前几天写了一篇文章:ThreadPoolExcutor 线程池 异常处理 (上篇) 中已经介绍了线程池异常的一些问题以及一步步分析了里面的一些源代码 ...

  6. ThreadPoolExecutor使用详解

    ThreadPoolExecutor机制  一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线 ...

  7. ThreadPoolExecutor参数

    1.ThreadPoolExecutor个参数的意义(类上的注释内容) * @param corePoolSize the number of threads to keep in the* pool ...

  8. dubbo超时重试和异常处理

    dubbo超时重试和异常处理 dubbo超时重试和异常处理 参考: https://www.cnblogs.com/ASPNET2008/p/7292472.html https://www.tuic ...

  9. 十七、ThreadPoolExecutor线程池

    一.简介 executor接口 executor接口在JDK的java.util.concurrent包下,它只有一个抽象方法: void execute(Runnable command); 这意味 ...

随机推荐

  1. redis的安全问题

    1.修改redis.conf配置文件 2.重启redis服务,使其生效 3.成功登陆以后,使用auth+密码 或者在登录的时候使用-a 密码的授权方式

  2. mysql if函数使用例子

    1.场景一 有时查询数量a 同时查询过滤后的数量b 2. 代码 SELECT count(id) as total_count, count( IF ( date(order_time) = DATE ...

  3. Web前端面试指导(十一):样式导入有哪些方式?

    样式导入方式 link import 使用方式 link的使用 <link href="index.css" rel="stylesheet"> i ...

  4. web前端优化之内容优化

    前端内容优化主要有以下几条: 1.尽量减少http请求 (1)合并文件,把多个css文件合并在一起: (2)css Sprites,把css相关的background元素进行背景图绝对定位: (3)图 ...

  5. Jave 之方法-函数(5)

    如何定义Java中的方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. (方法在C语言中被称为函数) 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问 ...

  6. memset用法详解

    原文:http://www.cnblogs.com/PegasusWang/archive/2013/01/20/2868824.html 1.void *memset(void *s,int c,s ...

  7. VS2013 C++ 动态链接库的生成

    原文:http://www.cnblogs.com/djiankuo/p/5092025.html 这个东西搞了好几天,现在终于没有问题了,其实现在想来还是微软做的东西好用啊,在这里点个赞!!! LL ...

  8. restful课程凌杂知识点

      request.post:字典形式数据 request.body:收到的是源数据

  9. GIT团队合作探讨之三--使用分支

    这篇文章是一个作为对git branch的综合介绍.首先,我们会看看创建branch,这有点像是请求一个新的项目历史.然后,我们看看git checkout是如何能够被用来选择一个branch,最后看 ...

  10. SQL Server ->> PERCENTILE_CONT、PERCENTILE_DISC 和 PERCENT_RANK 函数

    PERCENTILE_CONT和PERCENTILE_DISC都是为了计算百分位的数值,比如计算在某个百分位时某个栏位的数值是多少.他们的区别就是前者是连续型,后者是离散型.CONT代表continu ...