【JUC进阶】如何优雅地处理线程池异常?
迪丽瓦拉
2025-05-30 19:29:53
0

文章目录

  • 1. 线程池异常的出现
  • 2. 如何获取和处理异常
    • 2.1 使用try-catch
    • 2.2 使用Thread.setDefaultUncaughtExceptionHandler方法捕获异常
      • 2.2.1 submit和execute源码分析
    • 2.3 重写afterExecute进行异常处理

1. 线程池异常的出现

在开发中,我们经常使用线程池,会将不同的任务提交到线程池中,但是如果任务出现了异常,会发生什么呢?该怎么处理呢?怎么获取到异常信息来解决异常?

想要知道如何解决,就需要了解了解线程池提交任务的两个方法executesubmit

void execute(Runnable command);
Future submit(Runnable task);

由源码可见,两个任务最本质的区别就是execute无返回值,而submit由返回值。

接着用两个方法提交一个会抛出异常的任务看看发生什么?

public class Test {public static void main(String[] args) throws InterruptedException {//创建一个线程池ExecutorService executorService= Executors.newFixedThreadPool(1);//使用submit提交任务executorService.submit(new task());//使用execute提交任务executorService.execute(new task());}}
// 会抛出异常的任务
class task implements Runnable {@Overridepublic void run() {System.out.println("进入了task方法!!!");int i = 1 / 0;}
}

image-20230319142626880

由输出结果可见

  1. 当线程池抛出异常后 submit无提示,其他线程继续执行
  2. 当线程池抛出异常后 execute抛出异常,其他线程继续执行新任务

submit提交任务时,即使任务出现异常也不会打印异常信息,这是不友好的,这样开发者就不知道程序是否有异常。

其实,submit方法是将异常信息封装到其类型为Future的返回值中去了,想要获取异常信息,就必须使用get()方法

public static void main(String[] args) throws InterruptedException, ExecutionException {//创建一个线程池ExecutorService executorService= Executors.newFixedThreadPool(1);//使用submit提交任务Future submit = executorService.submit(new task());submit.get();//使用execute提交任务executorService.execute(new task());}

image-20230319143438770


2. 如何获取和处理异常


2.1 使用try-catch

在任务中可能出现异常的地方使用try-catch捕获异常,然后抛出。

class task implements Runnable {@Overridepublic void run() {System.out.println("进入了task方法!!!");try {int i = 1 / 0;}catch (Exception e){e.printStackTrace();}}
}

image-20230319143851527


2.2 使用Thread.setDefaultUncaughtExceptionHandler方法捕获异常

java.lang.Thread.setDefaultUncaughtExceptionHandler()方法设置处理程序时调用线程突然终止默认由于未捕获到异常,并没有其他的处理程序被定义为该线程。

public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {SecurityManager sm = System.getSecurityManager();if (sm != null) {sm.checkPermission(new RuntimePermission("setDefaultUncaughtExceptionHandler"));}defaultUncaughtExceptionHandler = eh;}

内部的uncaughtException是一个处理线程内发生的异常的方法,参数为线程对象t和异常对象e。

image-20230319144448284

因此,可以自己实现一个线程工厂,为每一个线程创建的线程设置UncaughtExceptionHandler对象 里面实现异常的默认逻辑。

public class Test {public static void main(String[] args) throws InterruptedException, ExecutionException {//1.实现一个自己的线程池工厂ThreadFactory factory = (Runnable r) -> {//创建一个线程Thread t = new Thread(r);//给创建的线程设置UncaughtExceptionHandler对象 里面实现异常的默认逻辑t.setDefaultUncaughtExceptionHandler((Thread thread1, Throwable e) -> {//出现异常if (e != null){e.printStackTrace();}});return t;};//2.创建一个自己定义的线程池,使用自己定义的线程工厂ExecutorService executorService = new ThreadPoolExecutor(1,1,0,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(10),factory);// submitFuture submit = executorService.submit(new task());submit.get();Thread.sleep(1000);System.out.println("==================为检验打印结果,1秒后执行execute方法");// executeexecutorService.execute(new task());}}

image-20230319145133900

这里使用submit提交任务时,控制台打印的异常信息,其实是因为submit.get();,如果没有调用get方法,控制台只会打印一条异常信息,也就是execute出现异常时候而打印的异常信息。

那么,就说明了submit的返回值内部存有异常信息,那么为什么使用submit提交的任务出现异常的时候,没有打印异常信息呢?

其实是因为submit方法内部已经捕获了异常, 只是没有打印出来,也因为异常已经被捕获,因此jvm也就不会去调用ThreadUncaughtExceptionHandler去处理异常。

这需要结合submit和execute的源码分析


2.2.1 submit和execute源码分析

submit源码中,可以看见,其实底层也是调用了execute方法,只是比execute封装多了一层RunnableFuture,而这个RunnableFuture就是submit的返回值。

public  Future submit(Callable task) {if (task == null) throw new NullPointerException();RunnableFuture ftask = newTaskFor(task);execute(ftask);return ftask;
}

而在execute中,当任务数量少于核心线程数的时候,会调用addWorker(command, true)为每个任务创建一个Worker去处理这些线程

public void execute(Runnable command) {if (command == null)throw new NullPointerException();int c = ctl.get();if (workerCountOf(c) < corePoolSize) {//任务数量少于核心线程数量if (addWorker(command, true))return;c = ctl.get();}if (isRunning(c) && workQueue.offer(command)) {int recheck = ctl.get();if (! isRunning(recheck) && remove(command))reject(command);else if (workerCountOf(recheck) == 0)addWorker(null, false);}else if (!addWorker(command, false))reject(command);
}

这个Worker也是一个线程,执行任务时调用的就是Workerrun方法!run方法内部又调用了runworker方法!:

private boolean addWorker(Runnable firstTask, boolean core) {.............Worker w = null;try {w = new Worker(firstTask);final Thread t = w.thread;......
}public void run() {runWorker(this);
}

可见,当使用execute提交任务的时候,会被封装成了一个runable任务,然后进去 再被封装成一个Worker,最后在workerrun方法里面调用runWoker方法,runWoker方法里面执行任务任务。

runWorker中,执行线程任务的是task.run();

如果任务出现异常,用try-catch捕获异常往外面抛,我们在最外层使用try-catch捕获到了 runWoker方法中抛出的异常。因此我们在execute中看到了我们的任务的异常信息。

final void runWorker(Worker w) {Thread wt = Thread.currentThread();Runnable task = w.firstTask;w.firstTask = null;w.unlock(); // allow interruptsboolean completedAbruptly = true;try {//这里就是线程可以重用的原因,循环+条件判断,不断从队列中取任务        //还有一个问题就是非核心线程的超时删除是怎么解决的//主要就是getTask方法()见下文③while (task != null || (task = getTask()) != null) {w.lock();if ((runStateAtLeast(ctl.get(), STOP) ||(Thread.interrupted() &&runStateAtLeast(ctl.get(), STOP))) &&!wt.isInterrupted())wt.interrupt();try {beforeExecute(wt, task);Throwable thrown = null;try {//执行线程task.run();//异常处理} catch (RuntimeException x) {thrown = x; throw x;} catch (Error x) {thrown = x; throw x;} catch (Throwable x) {thrown = x; throw new Error(x);} finally {//execute的方式可以重写此方法处理异常afterExecute(task, thrown);}} finally {task = null;w.completedTasks++;w.unlock();}}//出现异常时completedAbruptly不会被修改为falsecompletedAbruptly = false;} finally {//如果如果completedAbruptly值为true,则出现异常,则添加新的Worker处理后边的线程processWorkerExit(w, completedAbruptly);}}

submit方法t是将任务封装成了一个futureTask ,然后这个futureTask被封装worker成,在wokerrun方法里面,最终调用的是futureTaskrun方法,而在run方法里面,将异常吞掉了,并没有抛出异常,因此在workerrunWorker方法里面无法捕获到异常。

public void run() {if (state != NEW ||!UNSAFE.compareAndSwapObject(this, runnerOffset,null, Thread.currentThread()))return;try {Callable c = callable;if (c != null && state == NEW) {V result;boolean ran;try {result = c.call();ran = true;} catch (Throwable ex) {result = null;ran = false;//在此方法中设置了异常信息setException(ex);}if (ran)set(result);}//省略下文。。。。。。

run方法中,如果出现了异常,不会将异常往外抛,则是将异常设置给outcome

protected void setException(Throwable t) {if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {//将异常对象赋予outcome,记住这个outcome,outcome = t;UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final statefinishCompletion();}

submit方法返回值对象Future中,当调用Future.get()时, 会调用内部的report方法

public V get() throws InterruptedException, ExecutionException {int s = state;if (s <= COMPLETING)s = awaitDone(false, 0L);//注意这个方法return report(s);
}

report中返回值实际上就是上面的outcome

private V report(int s) throws ExecutionException {//设置`outcome`Object x = outcome;if (s == NORMAL)//返回`outcome`return (V)x;if (s >= CANCELLED)throw new CancellationException();throw new ExecutionException((Throwable)x);
}

因此,在使用submit方法提交任务时候,任务对象Runable会被封装程Future类型。

future 里面的 run方法在处理异常时, try-catch了所有的异常,通过setException(ex);方法设置到了变量outcome里面, 可以通过future.get获取到outcome

submit里面,除了从返回结果里面取到异常之外, 没有其他方法。因此,在不需要返回结果的情况下,最好用execute ,这样就算没有写try-catch,疏漏了异常捕捉,也不至于丢掉异常信息。


2.3 重写afterExecute进行异常处理

excute的方法里面,可以通过重写afterExecute进行异常处理,当然也适用于submit,但是因为submit的方式比较麻烦,submittask.run里面把异常吞了,根本不会跑出来异常,因此也不会有异常进入到afterExecute里面。

runWorker里面,调用task.run之后,会调用线程池的 afterExecute(task, thrown) 方法。

final void runWorker(Worker w) {
//当前线程Thread wt = Thread.currentThread();//我们的提交的任务Runnable task = w.firstTask;w.firstTask = null;w.unlock(); // allow interruptsboolean completedAbruptly = true;try {while (task != null || (task = getTask()) != null) {w.lock();if ((runStateAtLeast(ctl.get(), STOP) ||(Thread.interrupted() &&runStateAtLeast(ctl.get(), STOP))) &&!wt.isInterrupted())wt.interrupt();try {beforeExecute(wt, task);Throwable thrown = null;try {//直接就调用了task的run方法 task.run(); //如果是futuretask的run,里面是吞掉了异常,不会有异常抛出,// 因此Throwable thrown = null;  也不会进入到catch里面} catch (RuntimeException x) {thrown = x; throw x;} catch (Error x) {thrown = x; throw x;} catch (Throwable x) {thrown = x; throw new Error(x);} finally {//调用线程池的afterExecute方法 传入了task和异常afterExecute(task, thrown);}} finally {task = null;w.completedTasks++;w.unlock();}}completedAbruptly = false;} finally {processWorkerExit(w, completedAbruptly);}}

因此,我们在创建线程池的时候可以重写afterExecute方法

但是对于afterExecute处理submit提交的异常的时候,需要进行额外的处理,也就是判断Throwable是否是FutureTask

public class Test {public static void main(String[] args) throws InterruptedException, ExecutionException {//1.创建一个自己定义的线程池ExecutorService executorService = new ThreadPoolExecutor(2,3,0,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(10)) {//重写afterExecute方法@Overrideprotected void afterExecute(Runnable r, Throwable t) {//这个是excute提交的时候if (t != null) {System.out.println("afterExecute里面获取到excute提交的异常信息,处理异常" + t.getMessage());}//如果r的实际类型是FutureTask 那么是submit提交的,所以可以在里面get到异常if (r instanceof FutureTask) {try {Future future = (Future) r;//get获取异常future.get();} catch (Exception e) {System.out.println("afterExecute里面获取到submit提交的异常信息,处理异常" + e);}}}};//当线程池抛出异常后 executeexecutorService.execute(new task3());//当线程池抛出异常后 submitexecutorService.submit(new task3());}
}class task3 implements Runnable {@Overridepublic void run() {System.out.println("进入了task方法!!!");int i = 1 / 0;}
}

image-20230319161241336


参考:面试官:线程池中线程抛了异常,该如何处理?_Java精选的技术博客_51CTO博客

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
A.机器学习入门算法(三):基... 机器学习算法(三):K近邻(k-nearest neigh...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
Redis 所有支持的数据结构... Redis 是一种开源的基于键值对存储的 NoSQL 数据库,支持多种数据结构。以下是...
win下pytorch安装—c... 安装目录一、cuda安装1.1、cuda版本选择1.2、下载安装二、cudnn安装三、pytorch...
MySQL基础-多表查询 文章目录MySQL基础-多表查询一、案例及引入1、基础概念2、笛卡尔积的理解二、多表查询的分类1、等...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
MATLAB | 全网最详细网... 一篇超超超长,超超超全面网络图绘制教程,本篇基本能讲清楚所有绘制要点&#...
IHome主页 - 让你的浏览... 随着互联网的发展,人们越来越离不开浏览器了。每天上班、学习、娱乐,浏览器...
TCP 协议 一、TCP 协议概念 TCP即传输控制协议(Transmission Control ...
营业执照的经营范围有哪些 营业执照的经营范围有哪些 经营范围是指企业可以从事的生产经营与服务项目,是进行公司注册...
C++ 可变体(variant... 一、可变体(variant) 基础用法 Union的问题: 无法知道当前使用的类型是什...
血压计语音芯片,电子医疗设备声... 语音电子血压计是带有语音提示功能的电子血压计,测量前至测量结果全程语音播报࿰...
MySQL OCP888题解0... 文章目录1、原题1.1、英文原题1.2、答案2、题目解析2.1、题干解析2.2、选项解析3、知识点3...
【2023-Pytorch-检... (肆十二想说的一些话)Yolo这个系列我们已经更新了大概一年的时间,现在基本的流程也走走通了,包含数...
实战项目:保险行业用户分类 这里写目录标题1、项目介绍1.1 行业背景1.2 数据介绍2、代码实现导入数据探索数据处理列标签名异...
记录--我在前端干工地(thr... 这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前段时间接触了Th...
43 openEuler搭建A... 文章目录43 openEuler搭建Apache服务器-配置文件说明和管理模块43.1 配置文件说明...