Course contentsShow
← Java
Lesson 2728 of 2,87050. Executor FrameworkFree lesson

Thread Naming and Daemon Status in Factory Methods

How Executors factory methods create non-daemon threads with default naming patterns and when to customize ThreadFactory.

Thread Naming and Daemon Status in Factory Methods

You'll learn how Executors factory methods name threads and set daemon status, plus when to customize this behavior.

When you create thread pools using Executors.newFixedThreadPool(), newCachedThreadPool(), or newSingleThreadExecutor(), Java automatically creates threads with default characteristics. Understanding these defaults helps you debug issues and know when customization is necessary.

Default Thread Naming

Threads created by Executors factory methods receive generic names following the pattern pool-N-thread-M, where N is the pool number (incrementing across your application) and M is the thread number within that pool.

For example: pool-1-thread-1, pool-1-thread-2, pool-2-thread-1.

This naming is functional but unhelpful when debugging production issues with dozens of thread pools. Thread dumps become difficult to interpret when every pool looks identical.

Default Daemon Status

Factory methods create non-daemon threads by default. This means your JVM won't shut down until these threads finish, even after main() exits. Non-daemon threads keep your application alive, which is usually correct for work-processing pools—you want submitted tasks to complete.

When to Customize with ThreadFactory

You should provide a custom ThreadFactory to the ExecutorService constructor (like ThreadPoolExecutor) when you need:

  1. Meaningful names for debugging (e.g., "EmailSender-1", "ReportGenerator-2")
  2. Daemon threads for background housekeeping that shouldn't prevent JVM shutdown
  3. Custom thread priorities (though rarely needed)
  4. Uncaught exception handlers specific to your pool's tasks

Without a custom factory, thread dumps and monitoring tools show generic pool names, making troubleshooting harder in complex applications.

Key Takeaway:
Executors factory methods create non-daemon threads with generic pool-N-thread-M names—customize with a ThreadFactory when you need meaningful names for debugging or daemon behavior for background tasks.