面试官:线程池执行过程中遇到异常会发生什么,怎样处理

}

threadPoolExecutor.execute(() -> {

int j = 1/0;

});});

}

}

新建一个只有一个线程的线程池,每隔0.1s提交一个任务,任务中是一个1/0的计算。

Exception in thread “customThread 0” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

Exception in thread “customThread 1” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

Exception in thread “customThread 2” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

Exception in thread “customThread 3” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

Exception in thread “customThread 4” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

Exception in thread “customThread 5” java.lang.ArithmeticException: / by zero

at thread.ThreadExecutor.lambda$null$0(ThreadExecutor.java:25)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

可见每次执行的线程都不一样,之前的线程都没有复用。原因是因为出现了未捕获的异常。

我们把异常捕获试试:

public class ThreadExecutor {

private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,

new ArrayBlockingQueue<>(200), new ThreadFactoryBuilder().setNameFormat(“customThread %d”).build());

@Test

public void test() {

IntStream.rangeClosed(1, 5).forEach(i -> {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

threadPoolExecutor.execute(() -> {

try {

int j = 1 / 0;

} catch (Exception e) {

System.out.println(Thread.currentThread().getName() +" "+ e.getMessage());

}

});

});

}

}

customThread 0 / by zero

customThread 0 / by zero

customThread 0 / by zero

customThread 0 / by zero

customThread 0 / by zero

可见当异常捕获了,线程就可以复用了。另外, 多线程系列面试题和答案全部整理好了,微信搜索Java技术栈,在后台发送:面试,可以在线阅读。

问题来了,我们的代码中异常不可能全部捕获

如果要捕获那些没被业务代码捕获的异常,可以设置Thread类的uncaughtExceptionHandler属性。这时使用ThreadFactoryBuilder会比较方便,ThreadFactoryBuilder是guava提供的ThreadFactory生成器。

new ThreadFactoryBuilder()

.setNameFormat(“customThread %d”)

.setUncaughtExceptionHandler((t, e) -> System.out.println(t.getName() + “发生异常” + e.getCause()))

.build()

修改之后:

public class ThreadExecutor {

private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,

new ArrayBlockingQueue<>(200),

new ThreadFactoryBuilder()

.setNameFormat(“customThread %d”)

.setUncaughtExceptionHandler((t, e) -> System.out.println(“UncaughtExceptionHandler捕获到:” + t.getName() + “发生异常” + e.getMessage()))

.build());

@Test

public void test() {

IntStream.rangeClosed(1, 5).forEach(i -> {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

threadPoolExecutor.execute(() -> {

System.out.println(“线程” + Thread.currentThread().getName() + “执行”);

int j = 1 / 0;

});

});

}

}

线程customThread 0执行

UncaughtExceptionHandler捕获到:customThread 0发生异常/ by zero

线程customThread 1执行

UncaughtExceptionHandler捕获到:customThread 1发生异常/ by zero

线程customThread 2执行

UncaughtExceptionHandler捕获到:customThread 2发生异常/ by zero

线程customThread 3执行

UncaughtExceptionHandler捕获到:customThread 3发生异常/ by zero

线程customThread 4执行

UncaughtExceptionHandler捕获到:customThread 4发生异常/ by zero

可见,结果并不是我们想象的那样,线程池中原有的线程没有复用!所以通过UncaughtExceptionHandler想将异常吞掉使线程复用这招貌似行不通。它只是做了一层异常的保底处理。

推荐一个 Spring Boot 基础教程及实战示例:https://www.javastack.cn/categories/Spring-Boot/

将excute改成submit试试

public class ThreadExecutor {

private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,

new ArrayBlockingQueue<>(200),

new ThreadFactoryBuilder()

.setNameFormat(“customThread %d”)

.setUncaughtExceptionHandler((t, e) -> System.out.println(“UncaughtExceptionHandler捕获到:” + t.getName() + “发生异常” + e.getMessage()))

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

最后

金三银四马上就到了,希望大家能好好学习一下这些技术点

学习视频:

大厂面试真题:

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

中…(img-xGKuFa8P-1712778069219)]

最后

金三银四马上就到了,希望大家能好好学习一下这些技术点

学习视频:

[外链图片转存中…(img-gHjbMDuT-1712778069220)]

大厂面试真题:

[外链图片转存中…(img-gxaEhEe1-1712778069220)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-SExo6DWF-1712778069220)]

  • 21
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值