线程池中异常处理,解决多线程异常丢失问题

文章讲述了Java中线程池的submit和execute方法的区别,以及如何通过重写afterExecute方法统一处理submit和execute提交任务时的异常。submit返回Future,execute直接执行,需手动catch;通过重写afterExecute可以捕获所有异常并打印。
摘要由CSDN通过智能技术生成

线程池的提交方式有submit和execute

execute没有返回值,并且会打印异常信息

submit返回的是一个future ,默认情况不会打印异常信息,但可以通过这个future取到线程执行的结果或者异常信息:

        // submit
        Future<String> future = executorService.submit(() -> testService.dosth());
        try {
            future.get();
        } catch (Exception e) {
            log.error("submit 线程异常 ", e);
        }

或者使用try-catch

            // submit
            executorService.submit(() -> {
                try {
                    testService.dosth();
                } catch (Exception e) {
                    log.error("submit 线程异常 ", e);
                }
            });

submit源码在底层还是调用的execute方法,只不过多一层future封装,future里面的run方法try-catch了所有的异常,通过setException(ex)方法设置到了变量outcome里面, 可以通过future.get获取到outcome

解决办法

重写afterExecute方法一起处理所有异常信息

       // 创建线程池
        ExecutorService executorService = new ThreadPoolExecutor(
                4,
                4,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(64)) {
            // 重写afterExecute方法
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                // execute提交的时候
                if (t != null) {
                    log.error("execute 线程异常 ", t);
                }
                // submit提交的时候,Runnable实际类型是FutureTask,可以get到异常
                if (r instanceof FutureTask) {
                    try {
                        Future<?> future = (Future<?>) r;
                        //get获取异常
                        future.get();
                    } catch (Exception e) {
                        log.error("submit 线程异常 ", e);
                    }
                }
            }
        };

这两种提交方式都可以打印异常信息了 

        // submit
        executorService.submit(() -> testService.dosth());
 
        // execute
        executorService.execute(() -> testService.dosth());
    public String dosth() {
        int x = 1 / 0;
        return "Do ---> Success";
    }

日志 

10:13:53.185 [pool-1-thread-2] ERROR com.TestService - execute 线程异常 
java.lang.ArithmeticException: / by zero
	at com.TestService.dosth(TestService.java:53)
	at com.TestService.lambda$main$1(TestService.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
10:13:53.185 [pool-1-thread-1] ERROR com.TestService - submit 线程异常 
java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at com.TestService$1.afterExecute(TestService.java:37)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ArithmeticException: / by zero
	at com.TestService.dosth(TestService.java:53)
	at com.TestService.lambda$main$0(TestService.java:46)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	... 2 common frames omitted
Exception in thread "pool-1-thread-2" java.lang.ArithmeticException: / by zero
	at com.TestService.dosth(TestService.java:53)
	at com.TestService.lambda$main$1(TestService.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值