
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: June 11, 2024
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.
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());
}
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.
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.