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()))
.build());
@Test
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
章的可读性
Java面试宝典2021版
[外链图片转存中…(img-zowtp0Fe-1711200302044)]
[外链图片转存中…(img-utLM3ZsC-1711200302044)]
最常见Java面试题解析(2021最新版)
[外链图片转存中…(img-neyyVVM2-1711200302045)]
[外链图片转存中…(img-3dPY96mJ-1711200302045)]
2021企业Java面试题精选
[外链图片转存中…(img-dFRoEQkD-1711200302045)]
[外链图片转存中…(img-K16UuSXA-1711200302046)]