Multi-threaded thread pool exception handling
***********************************
Example: Exceptions are not output during thread pool execution, and solution test
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();
}
}
*************************
Related output
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)
Note: When executing synchronously (execute), the run () and run2 () method exceptions are output
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)
Note: When performing asynchronously (submit), the run () method does not output a runtime exception, and the run2 () method uses a try-catch statement block to output exception information
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)
Description: Custom thread pool, overloaded execute method, consistent with the output of unreloaded execute method
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)
Description: custom thread pool, overload submit method, run (), run2 () methods all output abnormal information
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)
Description: overload afterExecute method, synchronous execution, run () output and throw an exception, the thrown exception is output by afterExecute; run2 () output exception, no exception information is thrown
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)
Note: overloaded afterExecute method cannot handle exceptions during the execution of submit
Summary: The execute method exception can be output directly without processing; submit uses the try-catch statement block to handle the exception, or overload the submit method to handle