【121期】面试官问:线程池执行过程中遇到异常会发生什么,如何处理?

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

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

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

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

这个好理解,当线程出现未捕获异常的时候就执行不下去了,留给它的就是垃圾回收了。

线程池中线程频繁出现未捕获异常

当线程池中线程频繁出现未捕获的异常,那线程的复用率就大大降低了,需要不断地创建新线程。

做个实验:

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(() -> {

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)

可见每次执行的线程都不一样,之前的线程都没有复用。原因是因为出现了未捕获的异常。面试题:公众号Java精选,回复Java面试,获取近3000道面试题,支持在线随时随地刷题。

我们把异常捕获试试:

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

可见当异常捕获了,线程就可以复用了。

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

如果要捕获那些没被业务代码捕获的异常,可以设置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想将异常吞掉使线程复用这招貌似行不通。它只是做了一层异常的保底处理。

将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()))

.build());

@Test

public void test() {

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

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

Future<?> future = threadPoolExecutor.submit(() -> {

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

int j = 1 / 0;

});

try {

学习分享,共勉

这里是小编拿到的学习资源,其中包括“中高级Java开发面试高频考点题笔记300道.pdf”和“Java核心知识体系笔记.pdf”文件分享,内容丰富,囊括了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。同时还有Java进阶学习的知识笔记脑图(内含大量学习笔记)!

资料整理不易,读者朋友可以转发分享下!

Java核心知识体系笔记.pdf

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

中高级Java开发面试高频考点题笔记300道.pdf

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

架构进阶面试专题及架构学习笔记脑图

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

Java架构进阶学习视频分享

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
va开发面试高频考点题笔记300道.pdf**

[外链图片转存中…(img-O8IXa4P1-1713284395010)]

架构进阶面试专题及架构学习笔记脑图

[外链图片转存中…(img-u3IIGYPO-1713284395010)]

Java架构进阶学习视频分享

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-Gr4PPSip-1713284395011)]

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值