Course contentsShow
← Java
Lesson 2618 of 2,87048. Concurrency FundamentalsFree lesson

What Are Daemon Threads?

Learn the definition and purpose of daemon threads as background service threads in Java applications.

What Are Daemon Threads?

You'll learn how Java distinguishes between regular user threads and special background service threads called daemon threads.

The Core Concept

A daemon thread is a low-priority background thread that provides services to user threads. The key distinguishing characteristic: the JVM will not wait for daemon threads to complete before exiting. When all user threads finish executing, the JVM shuts down immediately, terminating any remaining daemon threads mid-execution, even if they haven't completed their work.

Think of daemon threads as support staff in a building. Once all the main workers (user threads) leave for the day, the building closes—even if the cleaning crew (daemon threads) hasn't finished. The building doesn't stay open just for support staff.

Purpose and Common Use Cases

Daemon threads are designed for background tasks that should not prevent your application from exiting. Typical examples include:

  • Garbage collection (the JVM's built-in daemon thread)
  • Monitoring or housekeeping tasks
  • Background logging or metrics collection
  • Timeout or watchdog services
  • Cache cleanup operations

These tasks are helpful while the application runs but aren't critical enough to delay application shutdown.

The Default Behavior

By default, threads you create inherit the daemon status of their parent thread. Since your main method runs in a user thread, any threads you create will also be user threads unless you explicitly change them. This means the JVM will wait for them to complete.

You cannot change a thread's daemon status after it has been started—the designation must be set before calling start().

Key Takeaway: Daemon threads are background service threads that the JVM terminates automatically when all user threads complete, making them ideal for non-critical support tasks that shouldn't prevent application shutdown.