wait until all threads finish their work in java
java, multithreading, wait
Solution
The approach I take is to use an ExecutorService to manage pools of threads.
ExecutorService es = Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
es.execute(new Runnable() { /* your task */ });
}
es.shutdown();
boolean finished = es.awaitTermination(1, TimeUnit.MINUTES);
// all tasks have finished or the time has been reached.
Problem
I'm writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class. I need to validate buffer data and store it in a database when all threads finished their job. How can I do this (get alerted when all threads finished their work) ?