间歇性头痛需要帮助。代码正在调用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 不应该清理这个执行程序,因为它还没有超出范围,但是从错误日志来看,这似乎是行为。
编辑:感谢您的快速回复。我可能需要澄清我也不能随意复制这个,否则我可能会有更多线索。代码通常运行良好,但这种错误很少发生,我们所拥有的只是我为症状粘贴的日志信息。
EDIT2:澄清我粘贴的代码不是我的。google-http-java-client
这是我正在使用的开源库。所以我没有办法改变它。我当然可以自己创建一个长期的Executor
并用第一种方法调用它,但我正在尝试了解现在的问题。