Skip to content

Instantly share code, notes, and snippets.

@netopyr
Created March 13, 2017 10:24
Show Gist options
  • Save netopyr/60ef6bc7bf857001380f070d021e2787 to your computer and use it in GitHub Desktop.
Save netopyr/60ef6bc7bf857001380f070d021e2787 to your computer and use it in GitHub Desktop.
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