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

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 {

future.get();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

}

});

}

}

线程customThread 0执行

java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

线程customThread 0执行

java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

线程customThread 0执行

java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

线程customThread 0执行

java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

线程customThread 0执行

java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

通过submit提交线程可以屏蔽线程中产生的异常,达到线程复用。当get()执行结果时异常才会抛出。面试题:公众号Java精选,回复Java面试,获取近3000道面试题,支持在线随时随地刷题。

原因是通过submit提交的线程,当发生异常时,会将异常保存,待future.get();时才会抛出。

这是Futuretask的部分run()方法,看setException:

public void run() {

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);

}

}

}

protected void setException(Throwable t) {

if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {

outcome = t;

UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state

finishCompletion();

}

}

我的面试宝典:一线互联网大厂Java核心面试题库

以下是我个人的一些做法,希望可以给各位提供一些帮助:

整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!

image

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

image

还有源码相关的阅读学习

image

一下!

[外链图片转存中…(img-Zpk9D27K-1714493169712)]

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

[外链图片转存中…(img-HVcHd1S5-1714493169713)]

还有源码相关的阅读学习

[外链图片转存中…(img-4B1k9GXV-1714493169713)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值