Custom named thread pool

Let's take a look at the DefaultThreadFactory used by java.util.concurrent.Executors in JDK1.6:

 /**
     * The default thread factory
     */
    static class DefaultThreadFactory implements ThreadFactory {
        static final AtomicInteger poolNumber = new AtomicInteger(1);
        final ThreadGroup group;
        final AtomicInteger threadNumber = new AtomicInteger(1);
        final String namePrefix;

        DefaultThreadFactory() {
            SecurityManager s = System.getSecurityManager();
            group = (s != null)? s.getThreadGroup() :
                                 Thread.currentThread().getThreadGroup();
            namePrefix = "pool-" +
                          poolNumber.getAndIncrement() +
                         "-thread-";
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                                  namePrefix + threadNumber.getAndIncrement(),
                                  0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }

Set a more meaningful thread name

The format of the thread name created in DefaultThreadFactory is pool-m-thread-n, that is, pool-1-thread-2,pool-2-thread-3. We can't see why the thread was created or what it was doing. It is inconvenient to debug, monitor, and view logs.

Especially when analyzing thread dump, the thread name is an important clue to confirm which Executor or thread pool the thread is created by and understand the thread information In the stack trace, it is often JDK class from the beginning to the end. It is difficult to know what this thread does. A meaningful thread name can help us quickly locate the problem. Let's try to write a thread factory with a name:

class MssThreadFactory implements ThreadFactory {
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    MssThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix+"-";
    }

    public Thread newThread(Runnable r) {
        Thread t = new Thread( r,namePrefix + threadNumber.getAndIncrement());
        if (t.isDaemon())
            t.setDaemon(true);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}

When used:

ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 10, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<>(),new MssThreadFactory("My own thread pool"));

for (int i = 0; i < 100; i++) {
    final int index = i;
    pool.submit(() -> System.out.println(Thread.currentThread().getName()));
}
  •  

Output results:

My own thread pool-3
My own thread pool-1
My own thread pool-3
My own thread pool-4
My own thread pool-7
My own thread pool-6
My exclusive thread pool-5
My own thread pool-2
My own thread pool-4
My own thread pool-3
My exclusive thread pool-1
My exclusive thread pool-9
My own thread pool-8
My exclusive thread Pool-10

The name and serial number of the custom thread pool are output for debugging and locating problems.

Tags: Programming Java JDK

Posted on Fri, 08 Nov 2019 04:55:37 -0500 by robbyc