-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Rationale
Why should this feature exist?
- At present, the java-tron
thread poolname is not clearly defined, a thread is created with the following (default) name: Thread[pool-1-thread-1], which isn’t helpful and doesn’t let you differentiate threads by name. As you can see, this isn't useful. - Some thread pools don't have a graceful shutdown.
What are the use-cases?
In case of performance issues: thread dumps and using the top command. In both cases, it’s useful to know what function does a thread service, as the stacktrace in the dump might not always be revealing.
A tool for quick investigation is top. More precisely, top -H -p . This shows the usual top table, but the -H flag means that threads for the chosen process should be printed. You basically get the most CPU-heavy and currently active threads, by name. In those cases, it’s extremely useful to have custom names.
Implementation
All of these executors have a thread factory. It’s hidden in the default factory method, but you can supply a thread factory. If not supplied, a default thread factory is used whenever a thread is needed.
- Name threads and thread-pools of ExecutorService.
ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("my-sad-thread-%d").build();
ExecutorService es = Executors.newFixedThreadPool(4, factory);- Make time-wait shutdown(awaitTermination) for thread-pools of ExecutorService.
es.shutdown();
es.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS);Are you willing to implement this feature?
Yes.