Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Understanding Daemon and User Threads in Java

Understanding Daemon and User Threads in Java

This article explains the concepts of daemon and user threads in Java, how to set a thread as daemon or user using Thread.setDaemon, the differences in lifecycle behavior, appropriate use cases, and includes example code demonstrating each type and their impact on JVM termination.

Basic Concepts

In Java there are two kinds of threads: daemon threads and user threads.

Daemon Thread: a special background thread that provides system services such as garbage collection or JIT compilation.

User Thread: the normal work thread that performs the business logic of the program; threads created with new Thread() are user threads by default.

How to Set a Thread as User or Daemon

Call Thread.setDaemon(false) to make it a user thread.

Call Thread.setDaemon(true) to make it a daemon thread.

If you do not set the property, the thread defaults to a user thread.

The daemon flag must be set before the thread is started; otherwise an IllegalThreadStateException is thrown.

Differences Between Daemon and User Threads

User Thread Example

public class UserThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println("I am a user thread......");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        Thread.sleep(3000);
        System.out.println("Main thread finished......");
    }
}

Running the above code produces the following output (image omitted).

The main thread ends, but the program does not exit because the user thread runs an infinite loop.

Daemon Thread Example

public class UserThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println("I am a user thread......");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // Set as daemon thread
        thread.setDaemon(true);
        thread.start();
        Thread.sleep(3000);
        System.out.println("Main thread finished......");
    }
}

Running this code produces the following output (image omitted).

The program terminates normally because the daemon thread does not keep the JVM alive after the main thread finishes.

Key Differences

After the main thread ends, user threads continue to run, keeping the JVM alive.

If there are no user threads—only daemon threads—the JVM exits and all daemon threads are terminated.

When all user threads have finished, the program’s business work is done, so the system can shut down; the JVM automatically exits when only daemon threads remain.

Typical Use Cases for Daemon Threads

Daemon threads are suitable for background services that support other user threads, such as heartbeat monitoring, event listening, and the most famous example in Java: garbage collection.

Summary

Java threads are divided into user threads and daemon threads.

When all user threads finish, the JVM exits regardless of the state of daemon threads.

Use Thread.setDaemon() to designate a thread as daemon.

The setDaemon() call must occur before Thread.start(); calling it later throws an exception and has no effect.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaConcurrencythreadsDaemon ThreadUser Thread

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion