June 2018
Intermediate to advanced
408 pages
11h 23m
English
The ThreadFactory interface is used to create a new thread on demand and also helps us to eliminate lots of boilerplate code for creating threads.
The following example shows how we can use the ThreadFactory interface to create new threads:
public class ThreadFactoryExample implements ThreadFactory { private static final Logger LOGGER = Logger.getLogger(ThreadFactoryExample.class); public static void main(String[] args) { ThreadFactoryExample factory = new ThreadFactoryExample(); Runnable task = new Runnable() { public void run() { LOGGER.info(Thread.currentThread().getName()); } }; for (int i = 0; i < 5; i++) { Thread t = factory.newThread(task); t.start(); } } @Override public Thread newThread(Runnable r) { Thread t = new ...Read now
Unlock full access