简体   繁体   中英

java.util.concurrent.RejectedExecutionException:

I was trying to do small poc to understand the difference between Futures.addCallback(future, callback) and Futures.addCallback(future, callback, executor1). As per my understanding, In the latter one the callback should be run by executor1 as specified in the function call. However while running the below code snippet, I am not able to figure out the reason for the above exception:

  class MyTest implements Callable<Integer>{

    int i;
    int j;

    public MyTest(int k, int l) {
        i = k;
        j = l;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
    }

    @Override
    public Integer call() throws Exception {
         System.out.println(Thread.currentThread().getName());  
        System.out.println("Computing.........");
        Thread.currentThread().sleep(5000);
        return i + j;
    }

}


Callable<Integer> call = new MyTest(1,2);
        ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
        ListeningExecutorService executor1 = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
        //ExecutorService executor = Executors.newFixedThreadPool(1);
        ListenableFuture<Integer> future = executor.submit(call);
        FutureCallback<Integer> callback = new FutureCallback<Integer>() {

            @Override
            public void onSuccess(Integer result) {
                System.out.println(Thread.currentThread().getName());   
                System.out.println("Completed Successfully and result is :" + result);

            }

            @Override
            public void onFailure(Throwable t) {
                System.out.println("Failed");

            }
        };

        Futures.addCallback(future, callback, executor1);
        executor.shutdown();
        executor1.shutdown();

Exception log

 ????? ??, ???? ?:??:?? ??????? com.google.common.util.concurrent.ExecutionList executeListener
    SEVERE: RuntimeException while executing runnable com.google.common.util.concurrent.Futures$5@37e5f7d7 with executor com.google.common.util.concurrent.MoreExecutors$ListeningDecorator@3d1d9ca7
    java.util.concurrent.RejectedExecutionException: Task com.google.common.util.concurrent.Futures$5@37e5f7d7 rejected from java.util.concurrent.ThreadPoolExecutor@39220f5b[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
        at com.google.common.util.concurrent.MoreExecutors$ListeningDecorator.execute(MoreExecutors.java:484)
        at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)
        at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145)
        at com.google.common.util.concurrent.ListenableFutureTask.done(ListenableFutureTask.java:91)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:384)
        at java.util.concurrent.FutureTask.set(FutureTask.java:233)
        at java.util.concurrent.FutureTask.run(FutureTask.java:274)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

Exception is throw because in you code snippet executor1 was stopped by call executor1.shutdown(); Try comment out this line and exception gone.

If you want to shutdown executor after callback completes execute shutdown inside callback code

  FutureCallback<Integer> callback = new FutureCallback<Integer>() {

        @Override
        public void onSuccess(Integer result) {
            System.out.println(Thread.currentThread().getName());   
            System.out.println("Completed Successfully and result is :" + result);
            executor1.shutdown(); // shutdown it here! 
        }

        @Override
        public void onFailure(Throwable t) {
            System.out.println("Failed");
            executor1.shutdown(); // shutdown it here! 
        }
    };
    Futures.addCallback(future, callback, executor1);
    executor.shutdown();
    // executor1.shutdown(); // do not stop it here!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM