36

I have a Java application that uses the Executor framework and I have code that looks like this protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5)

My understanding is that internally the JVM would create a pool of 5 threads. Now when I check the execution in a profiler, I get something like thread-pool2,thread-pool3 and so on.

Some of these thread pools are created by the server and some are created by me, I need a way to differentiate which were created by me and which were created by the server.

I am thinking that if I can name the thread pools it should do the trick, however do not see any API which would allow me to do the same.

Thanks in advance.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Sudarshan
  • 8,574
  • 11
  • 52
  • 74

4 Answers4

48

You can pass your own ThreadFactory to ScheduledThreadPoolExecutor. Your ThreadFactory will create thread and can give it any name you want. Your ThreadFactory can also reuse Executors.defaultThreadFactory(), and only change the name before returning the thread.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • 13
    If you're already using Guava, then its [`ThreadFactoryBuilder`](http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/concurrent/ThreadFactoryBuilder.html) can make that pretty easy. – Joachim Sauer Apr 21 '11 at 06:47
  • @Joachim: ooh, very nice. I do use Guava, but I missed that class. Thank you! – Peter Štibraný Apr 21 '11 at 06:49
  • Is giving a name for a thread in the thread pool, same as naming the thread pool itself ?, I need to name the thread pool so that I can start seeing that name in the profiler :) – Sudarshan Apr 21 '11 at 09:05
  • 1
    @Sudarshan: thread pool itself has no name. Only threads do have names. Profiler is showing threads to you, not the pool. – Peter Štibraný Apr 21 '11 at 09:26
  • Yup just realized that Thanks :) – Sudarshan Apr 21 '11 at 14:05
12
public class NamedThreadPoolExecutor extends ThreadPoolExecutor {

private static final String THREAD_NAME_PATTERN = "%s-%d";

    public NamedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, final TimeUnit unit,
                               final String namePrefix) {
       super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>(),
            new ThreadFactory() {

                private final AtomicInteger counter = new AtomicInteger();

                @Override
                public Thread newThread(Runnable r) {
                    final String threadName = String.format(THREAD_NAME_PATTERN, namePrefix, counter.incrementAndGet());
                    return new Thread(r, threadName);
                }
            });
    }

}
Karol Król
  • 3,320
  • 1
  • 34
  • 37
9

From the ThreadPoolExecutor documentation:

Creating new threads New threads are created using a ThreadFactory. If not otherwise specified, a Executors.defaultThreadFactory() is used, that creates threads to all be in the same ThreadGroup and with the same NORM_PRIORITY priority and non-daemon status. By supplying a different ThreadFactory, you can alter the thread's name, thread group, priority, daemon status, etc. If a ThreadFactory fails to create a thread when asked by returning null from newThread, the executor will continue, but might not be able to execute any tasks.

noamt
  • 7,397
  • 2
  • 37
  • 59
0

Use your own custom thread factory. Implement a ThreadFactoryBuilder to create you custom thread factories that allows you to do the following:

  1. Have custom thread names
  2. Have choice of threads - User or Daemon threads
  3. Have choice of Thread Priority
  4. Have flexibility to set uncaught exception handlers

You have a sample ThreadFactoryBuilder implementation in the following post which you can use.

http://wilddiary.com/understanding-java-threadfactory-creating-custom-thread-factories/

Drona
  • 6,886
  • 1
  • 29
  • 35