Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of a cached-thread-pool where all threads are daemon-threads
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class ImprovedCachedThreads {
public static void main(String[] args) {
final ThreadFactory threadFactory = runnable -> {
final Thread thread = new Thread(runnable, "HelloWorldThread");
thread.setDaemon(true);
return thread;
};
final ExecutorService executor = Executors.newCachedThreadPool(threadFactory);
executor.submit(() -> System.out.println("Hello World"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment