我是靠谱客的博主 大方海燕,最近开发中收集的这篇文章主要介绍多线程 线程池异常处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


多线程 线程池异常处理

 

***********************************

示例:线程池执行过程中异常不输出,及解决方法测试

 

public class MyTest {
public static Runnable run(){
return ()->System.out.println("2/0="+2/0);
}
public static Runnable run2(){
return ()-> {
try {
System.out.println("2/0="+2/0);
}catch (Exception e){
e.printStackTrace();
}
};
}
public static void fun(){
ExecutorService executorService= Executors.newFixedThreadPool(5);
executorService.execute(run());
executorService.execute(run2());
executorService.shutdown();
}
public static void fun2(){
ExecutorService executorService= Executors.newFixedThreadPool(5);
executorService.submit(run());
executorService.submit(run2());
executorService.shutdown();
}
public static void fun3(){
ExecutorService executorService=new ThreadPoolExecutor(5,5,20, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10)){
@Override
public void execute(Runnable command) {
try {
super.execute(command);
}catch (Exception e){
e.printStackTrace();
}
}
};
executorService.execute(run());
executorService.execute(run2());
executorService.shutdown();
}
public static void fun4(){
ExecutorService executorService=new ThreadPoolExecutor(5,5,10L,TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10)){
@Override
public Future<?> submit(Runnable task) {
return super.submit(()->{
try{
task.run();
}catch (Exception e){
e.printStackTrace();
}
});
}
};
executorService.submit(run());
executorService.submit(run2());
}
public static void fun5(){
ExecutorService executorService=new ThreadPoolExecutor(5,5,10l,TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10)){
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t!=null){
System.out.println("afterExecute:"+t.getMessage());
}
}
};
executorService.execute(run());
executorService.execute(run2());
executorService.shutdown();
}
public static void fun6(){
ExecutorService executorService=new ThreadPoolExecutor(5,5,10l,TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10)){
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t!=null){
System.out.println("afterExecute:"+t.getMessage());
}
}
};
executorService.submit(run());
executorService.submit(run2());
executorService.shutdown();
}
public static void main(String[] args){
fun();
//fun2();
//fun3();
//fun4();
//fun5();
//fun6();
}
}

 

*************************

相关输出

 

fun()

Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run$0(MyTest.java:8)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)
java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明:同步执行(execute)时,run()、run2()方法异常都有输出

 

fun2()

java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明:异步执行(submit)时,run()方法不输出运行时异常、run2()方法使用try-catch语句块输出异常信息

 

fun3()

Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run$0(MyTest.java:8)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)
java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明:自定义线程池,重载execute方法,与未重载execute方法执行输出一致

 

fun4()

java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run$0(MyTest.java:8)
at threadtest.MyTest$2.lambda$submit$0(MyTest.java:65)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)
java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at threadtest.MyTest$2.lambda$submit$0(MyTest.java:65)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明:自定义线程池,重载submit方法,run()、run2()方法都输出异常信息

 

fun5()

java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)
afterExecute:/ by zero
Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run$0(MyTest.java:8)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明: 重载afterExecute方法,同步执行,run()输出并抛出异常,抛出的异常由afterExecute输出;run2()输出异常,无异常信息抛出

 

fun6()

java.lang.ArithmeticException: / by zero
at threadtest.MyTest.lambda$run2$1(MyTest.java:14)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:835)

说明:重载afterExecute方法不能处理submit执行过程中的异常

 

总结:execute方法异常不需要处理可直接输出;submit使用try-catch语句块处理异常,

或者重载线程池ThreadPoolExecutor中的submit方法

 

 

最后

以上就是大方海燕为你收集整理的多线程 线程池异常处理的全部内容,希望文章能够帮你解决多线程 线程池异常处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部