I'm pretty new to Java multi threading, I've found a few responses for what I'm trying to do. However being new to java threading I'm still having a hard time following the responses.
Basically this is what I've got:
private final ExecutorService mFixedThreadPool;
public ThreadPool(int threadCount) {
mFixedThreadPool = Executors.newFixedThreadPool(threadCount);
}
public interface Task {
void phase1();
void phase2();
void phase3();
}
public void executeBatch(List<Runnable> tasks) {
tasks.forEach(task -> mFixedThreadPool.execute(task::phase1));
tasks.forEach(task -> mFixedThreadPool.execute(task::phase2));
tasks.forEach(task -> mFixedThreadPool.execute(task::phase3));
//only return on the main thread once all the tasks are complete.
//(Dont destroy threadpool as the "executeBatch" method will be called in a loop)
}
I want to pause or stop or wait on the thread that calls "executeBatch" until the batch of work is complete. I know it's possible to do this using mFixedThreadPool.shutdown() then waiting till its shutdown successfully, however I would like to reuse the threads many times very often so shutting down each time is inefficient.