I have question about ExecutorService. I have list of Status
, each of this status contains list of person that have this status. What i want to do is
I have this code
ExecutorService threadExecutor = Executors.newFixedThreadPool(10);
List<Status> statusList = getStatusList();
List<PersonInfoWrapper> personInfoWrapperList = new ArrayList<>();
for(Status status: statusList ){
List<PersonInfo> personInfo = status.getPersonList();
List<Future<PersonInfoWrapper> futures = new ArrayList<>();
for(PersonInfo info : personInfo){
Future<PersonInfoWrapper> future = threadExecutor.submit(() -> processInfo(info));
futures.add(future);
}
for(Future<PersonInfoWrapper> future : futures ){
personInfoWrapperList .add(future.get());
}
}
threadExecutor.shutdown();
My questions are:
The ExecutorService has threadpool of 10. However there are more persons than 10 for each status. When i process it concurrently ( as indicated in second for loop ), and then call future.get() in the third for loop, doesnt it block the execution of the other tasks in ExecutorService?
Is it okay to reuse ExecutorService like this? Shouldnt i use new executor service for every status i am about to process?
Future offers you method isDone() which is not blocking and returns true if computation has completed, false otherwise.
Future.get() is used to retrieve the result of computation.
You have a couple of options:
Hope, that helps.
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.