I am using an ExecutoreService in Java 1.6, started simply by
ExecutorService pool = Executors.newFixedThreadPool(THREADS).
When my main
If you have a known list of tasks, you don't need daemon threads at all. You can simply call shutdown() on the ExecutorService after submitting all your tasks.
When your main thread is complete, use the awaitTermination() method to allow time for the submitted tasks to complete.The currently submitted tasks will be executed, and the thread pool will terminate its control thread once they have been completed.
for (Runnable task : tasks) {
threadPool.submit(task);
}
threadPool.shutdown();
/*... do other stuff ...*/
//All done, ready to exit
while (!threadPool.isTerminated()) {
//this can throw InterruptedException, you'll need to decide how to deal with that.
threadPool.awaitTermination(1,TimeUnit.SECOND);
}