Naming Java threads

Naming the threads in a Java application is good practice as the default name given by a default thread pool looks like this: pool-x-thread-y.

Here is a simple implementation of a named thread factory:

public class NamedThreadFactory implements ThreadFactory {

    private final AtomicInteger threadIndex = new AtomicInteger(0);

    private final String name;

    public NamedThreadFactory(String name) { = name;

    public Thread newThread(Runnable runnable) {
        Thread thread = new Thread(runnable);
        thread.setName(String.format("%s-%d", name, threadIndex.addAndGet(1)));
        return thread;


Use it like this:

ExecutorService threadPool = Executors.newFixedThreadPool(
        numberOfThreads, new NamedThreadFactory("MyUsefulThreadName"));