マルチスレッドスレッドプールの例外処理


マルチスレッドスレッドプールの例外処理

 

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

例:スレッドプールの実行中に例外が出力されず、ソリューションテスト

 

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)

注:同期的に実行(実行)すると、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)

注:非同期で実行(送信)する場合、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)

説明:カスタムスレッドプール、オーバーロードされた実行メソッド、アンロードされた実行メソッドの出力と一致

 

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)

説明:カスタムスレッドプール、オーバーロード送信メソッド、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メソッドは、送信の実行中に例外を処理できません

 

概要:executeメソッドの例外は、処理せずに直接出力できます。submitは、try-catchステートメントブロックを使用して例外を処理するか、submitメソッドをオーバーロードして処理します

 

 

387の元の記事を公開 98のような 30,000以上の訪問

おすすめ

転載: blog.csdn.net/weixin_43931625/article/details/104959812