关于使用ExecutionCompletionService的正确方法,我有几个问题。我目前的实施如下:
INFORMATION_SCHEMA
我在调用int[] numThreads = new int[]{1,2,4,8,16};
for(int currThreads: numThreads) {
ExecutorCompletionService<Boolean> completionService;
ExecutorService myExecutors = Executors.newFixedThreadPool(currThreads);
completionService = new ExecutorCompletionService<Boolean>(myExecutors);
List<Future<Boolean>> futuresList = new ArrayList<>();
List<Boolean> myResults = new ArrayList<>();
try {
for(int i=0; i<currThreads; i++) {
futuresList.add(completionService.submit(
() -> {
//Some logic
return result;
}
));
}
for(int i=0; i<currThreads; i++) {
myresults.add(futuresList.get(i).get());
}
for(int i=0;i<myresults.size; i++) {
if(!myresults.get(i)){
//throw exception
}
}
} finally {
myResults.clear();
myExecutors.shutdown();
myExecutors.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
}
方法时遇到OutOfMemory错误。我在这里实施有什么问题?
与Future.get()
completionService.take().get()
会有所不同吗?
其次, 是否建议在for循环之外启动池?因为我知道我要产生的最大线程数。
以下是stacktrace的样子:
Future.get()