java - How to wait for all callables to finish executing before proceeding? -
i have following code hashed out:
public class mycallable implements callable<long> { @override public long call() throws exception { // stuff... } } public class mycontroller { private executorservice executor = executos.newcachedtreadpool(); public long concurrentdostuff() { list<mycallable> workers = makeworkers(); list<long> allresults = new arraylist<long>(); for(mycallable worker : workers) { future<long> workerresults = executor.submit(worker); try { allresults.add(workerresults.get()); } catch(interruptedexception ie) { // handle... } catch(executionexception ee) { // handle... } } // question: how pause here , wait workers finish? } } after for-loop, want wait workers finish before proceeding further. what's best/safest/most-efficient way this? in advance!
you must shut executor down shutdown , wait until jobs have been processed awaittermination.
Comments
Post a Comment