java - How to wait for all threads to finish, using ExecutorService? -
i need execute amount of tasks 4 @ time, this:
executorservice taskexecutor = executors.newfixedthreadpool(4); while(...) { taskexecutor.execute(new mytask()); } //...wait completion somehow
how can notified once of them complete? can't think better setting global task counter , decrease @ end of every task, monitor in infinite loop counter become 0; or list of futures , in infinite loop monitor isdone of them. better solutions not involving infinite loops?
thanks.
basically on executorservice
call shutdown()
, awaittermination()
:
executorservice taskexecutor = executors.newfixedthreadpool(4); while(...) { taskexecutor.execute(new mytask()); } taskexecutor.shutdown(); try { taskexecutor.awaittermination(long.max_value, timeunit.nanoseconds); } catch (interruptedexception e) { ... }
Comments
Post a Comment