I have a Java threadpool created via Executors.newFixedThreadPool() that I want to use daemon threads.
The reason is that this is a GUI app and I don’t want these threads to cause the program to stay running after the Window is closed.
I implemented a custom ThreadFactory that sets Thread.setDaemon(true) on the threads it creates.
The class is this:
import java.util.concurrent.ThreadFactory;
public class DaemonThreadFactory implements ThreadFactory{
public Thread newThread(Runnable runnable){
Thread thread = new Thread();
thread.setDaemon(true);
return thread;
}
}
For some reason when I use DaemonThreadFactory with Executors.newFixedThreadPool() none of my queued tasks are executed. If I change it back to regular ThreadFactory it works.
What am I doing wrong?
You’re not passing the
Runnableto the thread, so there’s no code to execute: