java - Correct way to get output from ExecutorCompletionService -


i'm using executorcompletionservice result of jobs completes execution. pseudocode this-

instantiate fixedthreadpool executor, exec instantiate executorcompletionservice, completionservice for(tasklist) {    completionservice.submit(sometask) } exec.shutdown(); whie(!exec.isshutdown()) { //line 1    task t = completionservice.take(); //line 2 } 

as know, shutdown() waits completion of submitted tasks , long there task in progress while condition on line1 returns true , code goes inside loop take completed task queue , waits if necessary.
now, i've observed problem above code in control gets blocked on line2 if there no more tasks in progress, tasks have been completed.
think it's because when last task completed , added completion queue, threadpoolexecutor resumes shutdown process there no more pending submitted tasks. while still shutting down, next iteration takes place , isshutdown() returns false goes inside loop , gets blocked take() call though there no more tasks. maybe it's time between last take call , pool shutting down properly. thinking in right direction?
thinking if right way use , result completionservice? fix this, can replace while on line1 for(tasklist) in order avoid isshutdown call. there's thing, best practice shutdown executor , result completionservice or first collect result , shutdown execturo in end?

try use isterminated() instead of isshutoown(). best choice use awaittermination() method instead of creating loop.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -