垃圾收集似乎关闭本地执行程序并导致RejectedExecutionException

问题描述 投票:4回答:1

需要帮助治疗间歇性头痛。代码调用com.google.api.client.http.HttpRequest#executeAsync(),它基本上有以下逻辑,

  @Beta
  public Future<HttpResponse> executeAsync(Executor executor) {
    FutureTask<HttpResponse> future = new FutureTask<HttpResponse>(new Callable<HttpResponse>() {

      public HttpResponse call() throws Exception {
        return execute();
      }
    });
    executor.execute(future);
    return future;
  }

  @Beta
  public Future<HttpResponse> executeAsync() {
    return executeAsync(Executors.newSingleThreadExecutor());
  }

这个调用有时会运行到java.util.concurrent.RejectedExecutionException,并且从日志看来,当这种情况发生时,它总是伴随着垃圾收集。下面是发生这种情况时的日志模式示例,

2017-09-26 11:04:56.039186 2017-09-26T11:04:56.012+0000: [GC pause (G1 Evacuation Pause) (young) 213M->50M(300M), 0.0262262 secs]
2017-09-26 11:04:56.048210 java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@71a0a39 rejected from java.util.concurrent.ThreadPoolExecutor@36c306aa[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
2017-09-26 11:04:56.048212      at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) ~[?:1.8.0_141]
2017-09-26 11:04:56.048214      at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) ~[?:1.8.0_141]
2017-09-26 11:04:56.048216      at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) ~[?:1.8.0_141]
2017-09-26 11:04:56.048218      at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:668) ~[?:1.8.0_141]
2017-09-26 11:04:56.048220      at com.google.api.client.http.HttpRequest.executeAsync(HttpRequest.java:1085) ~[google-http-client-1.21.0.jar:1.21.0]
2017-09-26 11:04:56.048222      at com.google.api.client.http.HttpRequest.executeAsync(HttpRequest.java:1099) ~[google-http-client-1.21.0.jar:1.21.0]

根据我的理解,GC不应该清理这个执行器,因为它还没有超出范围,但是根据错误的日志判断这似乎是行为。

编辑:感谢您的快速回复。我可能需要澄清我不能在我的遗嘱中重现这一点,否则我可能会有更多的线索。代码通常运行正常但这个错误确实很少发生,我们所拥有的只是为症状粘贴的日志信息。

编辑2:澄清我粘贴的代码不是我的。这是我正在使用的开源库google-http-java-client。所以我无法改变这一点。我肯定可以自己创建一个长期的Executor并用第一种方法调用它,但我正在试图理解现在的问题是什么。

java multithreading garbage-collection jvm google-http-client
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.