首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.util.concurrent.RejectedExecutionException:

java.util.concurrent.RejectedExecutionException:
EN

Stack Overflow用户
提问于 2015-03-10 22:19:55
回答 1查看 5.8K关注 0票数 0

我试着做一些小的poc来理解Futures.addCallback(未来,回调)和Futures.addCallback(未来,回调,executor1)之间的区别。根据我的理解,在后一种情况下,回调应该由函数调用中指定的executor1运行。但是,在运行下面的代码片段时,我无法找出上述异常的原因:

代码语言:javascript
复制
  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();

异常日志

代码语言:javascript
复制
 ????? ??, ???? ?:??:?? ??????? 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)
EN

回答 1

Stack Overflow用户

发布于 2015-03-11 21:33:10

异常被抛出,因为在您的代码片段中,通过调用executor1.shutdown();来停止executor1,尝试注释掉此行,然后异常就消失了。

如果要在callback完成后关闭executor,请在回调代码中执行shutdown

代码语言:javascript
复制
  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!
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28966175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档