首页 > 其他 > 详细

invokeAny()与全部异常

时间:2021-04-04 22:50:49      阅读:28      评论:0      收藏:0      [点我收藏+]

(1)

 1 package Seven_invokeAny_All_Demo1;
 2 
 3 import java.util.concurrent.Callable;
 4 
 5 public class MyCallableA implements Callable<String> {
 6 
 7     @Override
 8     public String call() throws Exception {
 9         System.out.println("MyCallableA" + Thread.currentThread().getName() + " begin " + System.currentTimeMillis());
10 
11         for (int i = 0; i < 123; i++) {
12             String newString = new String();
13             Math.random();
14             Math.random();
15             Math.random();
16             System.out.println("MyCallableA 在运行中= " + (i + 1));
17         }
18 
19         System.out.println("MyCallableA  " + Thread.currentThread().getName() + " end " + System.currentTimeMillis());
20 
21         if (1 == 1) {
22             System.out.println("1==1");
23             throw new Exception("线程A 报错了AAAAA");
24         }
25 
26         return "return---A";
27     }
28 
29 }
 1 package Seven_invokeAny_All_Demo1;
 2 
 3 import java.util.concurrent.Callable;
 4 
 5 public class MyCallableB implements Callable<String> {
 6 
 7     @Override
 8     public String call() throws Exception {
 9         System.out.println("MyCallableB" + Thread.currentThread().getName() + " begin " + System.currentTimeMillis());
10         for (int i = 0; i < 223; i++) {
11             String newString = new String();
12             Math.random();
13             Math.random();
14             Math.random();
15             System.out.println("MyCallableB 在运行中= " + (i + 1));
16         }
17 
18         System.out.println("MyCallableB  " + Thread.currentThread().getName() + " end " + System.currentTimeMillis());
19 
20         if (1 == 1) {
21             System.out.println("1==1");
22             throw new Exception("线程B 报错了BBBBB");
23         }
24 
25         return "return---B";
26     }
27 
28 }
 1 package Seven_invokeAny_All_Demo1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.concurrent.ExecutionException;
 6 import java.util.concurrent.ExecutorService;
 7 import java.util.concurrent.Executors;
 8 
 9 public class Run {
10 
11     public static void main(String[] args) {
12         try {
13 
14             List list = new ArrayList();
15 
16             list.add(new MyCallableB());
17             list.add(new MyCallableA());
18 
19             ExecutorService service = Executors.newCachedThreadPool();
20 
21             String getString = (String) service.invokeAny(list);;
22 
23             System.out.println("main取得返回值=" + getString);
24             
25             System.out.println("main---end");
26         } catch (InterruptedException e) {
27             System.out.println("进入   catch InterruptedException");
28             e.printStackTrace();
29         } catch (ExecutionException e) {
30             System.out.println("进入   ExecutionException");
31             e.printStackTrace();
32         }
33 
34     }
35 
36 }

运行结果:

技术分享图片          

 

技术分享图片

 

分析:都出现异常时,返回最后一个异常

(2)执行快的线程A中用try_catch捕获异常,但是不重新抛出

 1 package Seven_invokeAny_All_Demo1;
 2 
 3 import java.util.concurrent.Callable;
 4 
 5 public class MyCallableA implements Callable<String> {
 6 
 7     @Override
 8     public String call() throws Exception {
 9         
10         try{
11         System.out.println("MyCallableA" + Thread.currentThread().getName() + " begin " + System.currentTimeMillis());
12 
13         for (int i = 0; i < 123; i++) {
14             String newString = new String();
15             Math.random();
16             Math.random();
17             Math.random();
18             System.out.println("MyCallableA 在运行中= " + (i + 1));
19         }
20 
21         System.out.println("MyCallableA  " + Thread.currentThread().getName() + " end " + System.currentTimeMillis());
22 
23         if (1 == 1) {
24             System.out.println("1==1");
25             throw new Exception("线程A 报错了AAAAA");
26         }
27         }catch(Exception e){
28             System.out.println(e.getMessage() +"  :左边的信息就是捕获到的信息!!!");
29         }
30 
31         return "return---A";
32     }
33 
34 }

运行结果:

技术分享图片

 

技术分享图片

分析:线程A的异常通过try_catch捕获到了,但是没有重新抛出,导致主线程误认为线程A是没有问题的,所以即使A有问题,但是还是获取到了A的返回值

技术分享图片

(3)将A线程的异常try_catch了,然后再重新抛出:

package Seven_invokeAny_All_Demo1;

import java.util.concurrent.Callable;

public class MyCallableA implements Callable<String> {

    @Override
    public String call() throws Exception {
        
        try{
        System.out.println("MyCallableA" + Thread.currentThread().getName() + " begin " + System.currentTimeMillis());

        for (int i = 0; i < 123; i++) {
            String newString = new String();
            Math.random();
            Math.random();
            Math.random();
            System.out.println("MyCallableA 在运行中= " + (i + 1));
        }

        System.out.println("MyCallableA  " + Thread.currentThread().getName() + " end " + System.currentTimeMillis());

        if (1 == 1) {
            System.out.println("1==1");
            throw new Exception("线程A 报错了AAAAA");
        }
        }catch(Exception e){
            System.out.println(e.getMessage() +"  :左边的信息就是捕获到的信息!!!");
            throw e;//重新抛出异常
        }

        return "return---A";
    }

}

运行结果:

技术分享图片

分析:结果和(1)一样

 

invokeAny()与全部异常

原文:https://www.cnblogs.com/Leeyoung888/p/14616634.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!