How to use invokeAll() to let all thread pool do their task?

前端 未结 3 1276
失恋的感觉
失恋的感觉 2020-11-29 03:15
    ExecutorService pool=Executors.newFixedThreadPool(7);
        List> future=new ArrayList>();
        List<         


        
3条回答
  •  感动是毒
    2020-11-29 03:35

    Future.get() throws below exceptions.

    CancellationException - if the computation was cancelled

    ExecutionException - if the computation threw an exception

    InterruptedException - if the current thread was interrupted while waiting

    Catch all these exceptions when you call get() method.

    I have simulated divide by zero exception for some Callable tasks but exception in one Callable does not affect other Callable tasks submitted to ExecutorService if you catch above three exceptions as shown in example code.

    Example code snippet:

    import java.util.concurrent.*;
    import java.util.*;
    
    public class InvokeAllUsage{
        public InvokeAllUsage(){
            System.out.println("creating service");
            ExecutorService service = Executors.newFixedThreadPool(10);
    
            List futureList = new ArrayList();
            for ( int i=0; i<10; i++){
                MyCallable myCallable = new MyCallable((long)i+1);
                futureList.add(myCallable);
            }
            System.out.println("Start");
            try{
                List> futures = service.invokeAll(futureList);  
                for(Future future : futures){
                    try{
                        System.out.println("future.isDone = " + future.isDone());
                        System.out.println("future: call ="+future.get());
                    }
                    catch (CancellationException ce) {
                        ce.printStackTrace();
                    } catch (ExecutionException ee) {
                        ee.printStackTrace();
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt(); // ignore/reset
                    }
                }
            }catch(Exception err){
                err.printStackTrace();
            }
            System.out.println("Completed");
            service.shutdown();
        }
        public static void main(String args[]){
            InvokeAllUsage demo = new InvokeAllUsage();
        }
        class MyCallable implements Callable{
            Long id = 0L;
            public MyCallable(Long val){
                this.id = val;
            }
            public Long call(){
    
                if ( id % 5 == 0){
                    id = id / 0;
                }           
                return id;
            }
        }
    }
    

    output:

    creating service
    Start
    future.isDone = true
    future: call =1
    future.isDone = true
    future: call =2
    future.isDone = true
    future: call =3
    future.isDone = true
    future: call =4
    future.isDone = true
    java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
            at java.util.concurrent.FutureTask.report(FutureTask.java:122)
            at java.util.concurrent.FutureTask.get(FutureTask.java:188)
            at InvokeAllUsage.(InvokeAllUsage.java:20)
            at InvokeAllUsage.main(InvokeAllUsage.java:37)
    Caused by: java.lang.ArithmeticException: / by zero
            at InvokeAllUsage$MyCallable.call(InvokeAllUsage.java:47)
            at InvokeAllUsage$MyCallable.call(InvokeAllUsage.java:39)
            at java.util.concurrent.FutureTask.run(FutureTask.java:262)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:744)
    future.isDone = true
    future: call =6
    future.isDone = true
    future: call =7
    future.isDone = true
    future: call =8
    future.isDone = true
    future: call =9
    future.isDone = true
    java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
            at java.util.concurrent.FutureTask.report(FutureTask.java:122)
            at java.util.concurrent.FutureTask.get(FutureTask.java:188)
            at InvokeAllUsage.(InvokeAllUsage.java:20)
            at InvokeAllUsage.main(InvokeAllUsage.java:37)
    Caused by: java.lang.ArithmeticException: / by zero
            at InvokeAllUsage$MyCallable.call(InvokeAllUsage.java:47)
            at InvokeAllUsage$MyCallable.call(InvokeAllUsage.java:39)
            at java.util.concurrent.FutureTask.run(FutureTask.java:262)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:744)
    Completed
    

提交回复
热议问题