Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadFactory usage in Java

Can someone briefly explain on HOW and WHEN to use a ThreadFactory? An example with and without using ThreadFactory might be really helpful to understand the differences.

Thanks!

like image 972
jagamot Avatar asked Jul 05 '10 13:07

jagamot


People also ask

What is the use of Threadfactory?

An object that creates new threads on demand. Using thread factories removes hardwiring of calls to new Thread , enabling applications to use special thread subclasses, priorities, etc.

What is thread pool in java?

What is ThreadPool in Java? A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing.

What is executor in Java?

Interface ExecutorAn object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads.

What is ScheduledThreadPoolExecutor?

ScheduledThreadPoolExecutor class in Java is a subclass of ThreadPoolExecutor class defined in java. util. concurrent package. As it is clear from its name that this class is useful when we want to schedule tasks to run repeatedly or to run after a given delay for some future time. It creates a fixed-sized Thread Pool.


1 Answers

Here's one possible usage:

Assume you have an ExecutorService which executes your Runnable tasks in a multithreaded fashion, and once in a while a thread dies from an uncaught exception. Let's also assume that you want to log all of these exceptions. ThreadFactory solves this problem by allowing you to define a uniform logger for uncaught exceptions in the Runnable that the thread was executing:

ExecutorService executor = Executors.newSingleThreadExecutor(new LoggingThreadFactory());  executor.submit(new Runnable() {    @Override    public void run() {       someObject.someMethodThatThrowsRuntimeException();    } }); 

LoggingThreadFactory can be implemented like this one:

public class LoggingThreadFactory implements ThreadFactory {      @Override     public Thread newThread(Runnable r)     {         Thread t = new Thread(r);          t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()         {             @Override             public void uncaughtException(Thread t, Throwable e)             {                 LoggerFactory.getLogger(t.getName()).error(e.getMessage(), e);             }         });          return t;     } } 

The ThreadFactory interface is a flexible interface that allows the programmer to handle uncaught exceptions as shown above, but also allows much more control over the creation details of a Thread (like defining a pattern for the thread name) making it quite useful for debugging purposes and production environments alike.

like image 131
Boris Pavlović Avatar answered Oct 14 '22 20:10

Boris Pavlović