Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Daemon threads in Java are background threads that support other tasks, like system garbage collection, logging, system monitoring, and more. They run with lower priority and the Java Virtual Machine (JVM) terminates them after all user threads finish.  Many JVM threads are daemon threads by default.

In this short article, we’ll explore the main uses of daemon threads, and compare them to user threads. Additionally, we’ll demonstrate how to programmatically create, run, and verify if a thread is a daemon thread.

2. Creating Daemon Threads

To create daemon threads, all we need to do is to call setDaemon() of the Thread API. In this example, we’ll use the NewThread class which extends the Thread class:

NewThread daemonThread = new NewThread();
daemonThread.setDaemon(true);
daemonThread.start();

Any thread inherits the daemon status of the thread that created it. Since the main thread is a user thread, any thread created inside the main method is by default a user thread.

The method setDaemon() can only be called after the Thread object has been created and the thread hasn’t been started. An attempt to call setDaemon() while a thread is running throws an IllegalThreadStateException:

@Test(expected = IllegalThreadStateException.class)
public void whenSetDaemonWhileRunning_thenIllegalThreadStateException() {
    NewThread daemonThread = new NewThread();
    daemonThread.start();
    daemonThread.setDaemon(true);
}

Additionally, we can use the isDaemon() method to check if a thread is a daemon thread:

@Test
public void whenCallIsDaemon_thenCorrect() {
    NewThread daemonThread = new NewThread();
    daemonThread.setDaemon(true);
    daemonThread.start();
    assertTrue(daemonThread.isDaemon());

    NewThread userThread = new NewThread();
    userThread.start();
    assertFalse(userThread.isDaemon());
}

3. Differences Between Daemon and User Threads

So, we learned that Java offers two types of platform threads: user threads and daemon threads. While user threads are high-priority threads, daemon threads are low-priority threads whose only role is to provide services to user threads.

Let’s highlight other differences between daemon and user threads:

Feature Daemon Threads User Threads
Priority Lower priority, mainly for background services Higher priority, for main application tasks
JVM Behavior JVM exits when only daemon threads are running JVM continues running as long as any user thread is alive
Typical Use Cases Garbage collection, system monitoring, etc. Application logic, main program flow
Lifecycle Automatically terminates when all user threads finish Needs to be manually terminated

Since daemon threads are meant to serve user threads and are only needed while they are running, they won’t prevent the JVM from exiting once all user threads have finished their execution.

Infinite loops typically exist in daemon threads and won’t cause problems. This is happening because any code, including the finally blocks, won’t be executed once all user threads have finished their execution. As a result, daemon threads aren’t  recommended for I/O tasks.

However, there are exceptions to this rule. Poorly designed code in daemon threads can prevent the JVM from exiting. For example, calling Thread.join() on a running daemon thread can block the application’s shutdown.

4. Conclusion

In this quick tutorial, we’ve seen what daemon threads are and what they can be used for in a few practical scenarios. Following that, we compared daemon threads to user threads, focusing on their differences.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW