Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Java ThreadFactory


How to use Java Threadfactory to create custom threads and hide thread configuration details? In this post we’ll show how to create daemon threads with custom priorities.


Java 5 came with excellent java.util.concurrent, which contains ThreadFactory interface with only one method:

  • Thread newThread(Runnable r)
    Constructs a new Thread.

Of course the package ships many ready-to-use implementations and ThreadFactory is no different. java.util.concurrent.Executors has method defaultThreadFactory() that provides convenient implementation.

In this post we’re going to implement own ThreadFactory that creates daemon threads with given priorities. Before using the given priority we have to validate that it has acceptable value for Java Threads:


import java.util.concurrent.ThreadFactory;

import static java.lang.Thread.MAX_PRIORITY;
import static java.lang.Thread.MIN_PRIORITY;

public class ThreadFactoryExample {

    private static class EchoJob implements Runnable {
        private static int njobs = 0;
        private final int id = ++njobs;

        public void run() {
            System.out.println("Running job: " + id);

    private static class PrioritizedDaemonThreadFactory implements ThreadFactory {
        private static final String INVALID_PRIORITY_MESSAGE
                = "Priority must be between " + MIN_PRIORITY
                + " and " + MAX_PRIORITY + ". Was: ";
        private final int priority;

        public PrioritizedDaemonThreadFactory(int priority) {
            this.priority = priority;

        private void validatePriority(int priority) {
            if (priority < MIN_PRIORITY || MAX_PRIORITY < priority) {
                throw new IllegalArgumentException(
                        INVALID_PRIORITY_MESSAGE + priority);

        public Thread newThread(Runnable job) {
            System.out.println("Creating a new thread");
            Thread thread = new Thread(job);
            return thread;

    public static void main(String[] args) {
        System.out.println("Creating ThreadFactory with invalid priority:");
        try {
            new PrioritizedDaemonThreadFactory(0);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage() + "\n");

        System.out.println("Creating ThreadFactory producing slow daemons");
        ThreadFactory slowDaemons = new PrioritizedDaemonThreadFactory(MIN_PRIORITY);


        System.out.println("End of program.");

    private static void runJobs(ThreadFactory threadFactory) {
        System.out.println("Starting jobs:");
        for (int i = 0; i < 5; ++i) {
            threadFactory.newThread(new EchoJob()).start();

Using a Factory to create a new threads separates consumer of the threads (here runJobs(ThreadFactory)) from details of its creation (implementation of the factory). Now the type of created threads can be easily changed, for example one can use Executors.defaultThreadFactory().

Here’s the output of running the above code:

Creating ThreadFactory with invalid priority:
Priority must be between 1 and 10. Was: 0

Creating ThreadFactory producing slow daemons
Starting jobs:
Creating a new thread
Creating a new thread
Running job: 1
Creating a new thread
Running job: 2
Creating a new thread
Running job: 3
Creating a new thread
Running job: 4
End of program.

As can be seen in the output, the program ends before running the last job (5), which is perfectly fine for daemon threads (see Java daemon thread for more details).

Share with the World!