多线程 线程池异常处理


多线程 线程池异常处理

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

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

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语句块处理异常,或者重载submit方法进行处理

发布了387 篇原创文章 · 获赞 98 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/104959812