java - Waiting for all the tasks to finish -
java - Waiting for all the tasks to finish -
i have series of different "tasks" done using same thread pool. want measure time takes perform each task, need wait every task in "task" (sorry ambiguity) finish.
when there's 1 task this:
executorservice e = executors.newcachedthreadpool(); (int i=0; i<100; ++i) e.submit(target); e.shutdown(); while (!e.isterminated());
but since there several task submitted pool, can't down. methods have waiting tasks finish mention "after shutdown request". well, if don't want shut down, wait threads finish , submit more tasks?
this want do:
executorservice e = executors.newcachedthreadpool(); (int i=0; i<100; ++i) e.submit(target); // wait targets finish (int i=0; i<100; ++i) e.submit(target); // submit different tasks // wait... , on
i thought of shutting pool downwards , "waking up" 1 time again using prestartallcorethreads
, realized not executorservice
method threadpoolexecutor
method. solution? shutting down, waiting, , activating pool again? seems bit ugly me.
i thought natural thing utilize cyclicbarrier
, seems specific way of doing this, while think logical thing able utilize executorservice
i'm trying do.
is there way stick executorservice
s , wait tasks finish?
use cyclicbarrier
work need :
// optionalrunnable can collect info gathered tasks cyclicbarrier b = new cyclicbarrier(numberoftasks,optionalrunnable) task yourtaks = new task(...., b); // within run method phone call b.await() after work done; executor.submit(yourtaks);
optionally , can phone call await in main thread , instantiate barrier numtasks + 1 . way sure you're resubmitting tasks executor after it's done processing current batch
java multithreading concurrency threadpool
Comments
Post a Comment