DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Keeping AI-Powered BI Honest: A Human-in-the-Loop (HITL) Playbook
  • Amazon CodeWhisperer to Q Developer to Kiro: The Rise of Agentic Coding
  • When Valid SQL Was Still the Wrong Answer
  • The Rise of Microservices Architecture in Scalable Applications
  1. DZone
  2. Coding
  3. Frameworks
  4. Always Name Your Thread Pools

Always Name Your Thread Pools

Make life a little easier for the non-Spring folk.

By 
Bozhidar Bozhanov user avatar
Bozhidar Bozhanov
·
Mar. 24, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.1K Views

Join the DZone community and get the full member experience.

Join For Free

Our software tends to use a lot of thread pools – mostly through java.util.concurrent.ExecutorService implementations (Created via Executors.new.... We create these for various async use-cases, and they can be seen all over the place. 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.

When using Spring, those can be created using <task:executor />. In that case, each executor service’s thread factory is provided by Spring and it uses the name of the executor bean (specified with id="executorName"). But for those not created by Spring, a default name is used, which isn’t helpful and doesn’t let you differentiate threads by name.

And you need to differentiate threads by name – in case of performance issues you have various options to investigate: 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.

And my favorite tool for quick investigation is top. More precisely, top -H -p <pid>. 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.

But how do you set a name? By specifying a named thread factory when creating each executor. Here’s a StackOverflow answer with multiple ways to achieve thread naming.

The method that I’m using is based on the 2nd answer:

Java
 




x


 
1
public class AsyncUtils {
2
    public static ThreadFactory createNamedThreadFactory(String name) {
3
        return new ThreadFactoryBuilder().setNameFormat(name + "-%d").build();
4
    }
5
}



Centrally managing all executors through Spring might be a better idea, but not everyone is using Spring and sometimes an executor is needed for a small piece of functionality that could even go outside Spring Beans. So it’s a good idea to have that method up your sleeve.

Spring Framework

Published at DZone with permission of Bozhidar Bozhanov. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook