Executors and thread pools question 87 of 180 junior
Why name your threads, and how?
- Naming has no runtime effect
- Correct:
pool-3-thread-7tells you nothing in a dump, and a factory also sets daemon status and handlers - To make threads faster
- Because the JVM requires unique names
Thread names are the primary label in thread dumps, profilers, APM traces and log MDCs. When
production is on fire and the dump shows 200 threads called pool-2-thread-N, you cannot tell which
subsystem is stuck. payment-callback-7 answers the question instantly.
A ThreadFactory is also where you centralise the rest: daemon status, an UncaughtExceptionHandler
that logs rather than prints, a context classloader, and a per-pool counter. Any small library or a
handful of lines will do, and it costs nothing at runtime.
Quiz yourself on Executors and thread pools Suggest a correction