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

Checking Daemon Status

Learn how to use isDaemon() to check if a thread is configured as a daemon thread.

Checking Daemon Status

What you'll learn: How to determine whether a thread is currently configured as a daemon thread using the isDaemon() method.

After setting a thread as a daemon (or leaving it as a user thread), you'll often need to verify its status—especially when debugging thread lifecycle issues or validating configuration logic. Java provides the isDaemon() method for this exact purpose.

The isDaemon() Method

The isDaemon() method is called on a Thread instance and returns a boolean value:

  • Returns true if the thread is a daemon thread
  • Returns false if the thread is a user thread

Syntax:

boolean isDaemon = thread.isDaemon();

When to Check Daemon Status

You typically check daemon status in these scenarios:

  1. Validation: Confirming a thread was properly configured before starting it
  2. Debugging: Understanding why the JVM isn't terminating (user threads still running)
  3. Logging: Recording thread type information for diagnostics
  4. Conditional Logic: Making decisions based on whether a thread is daemon or user

Example Pattern

Thread backgroundTask = new Thread(() -> {
    // some work
});

backgroundTask.setDaemon(true);

// Verify configuration
if (backgroundTask.isDaemon()) {
    System.out.println("Confirmed: backgroundTask is a daemon thread");
}

backgroundTask.start();

You can also check the current thread's status:

if (Thread.currentThread().isDaemon()) {
    System.out.println("I'm running in a daemon thread");
}

Key Takeaway: Use isDaemon() to inspect whether a thread is configured as a daemon thread—helpful for validation, debugging, and making runtime decisions about thread behavior.