Fundamentals 6 min read

Understanding Daemon and Non‑Daemon Threads in Java with Code Examples

This article explains the concepts of daemon and non‑daemon (user) threads in Java, outlines their differences, and provides clear code demonstrations showing how daemon threads terminate with the main thread while user threads continue running independently after the main thread ends.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Daemon and Non‑Daemon Threads in Java with Code Examples

What Are Daemon and Non‑Daemon Threads?

In Java, threads can be classified into two states: daemon threads (e.g., the garbage‑collection thread) and non‑daemon or user threads that are created manually by developers.

Differences Between Daemon and Non‑Daemon Threads

Daemon Thread

Runs alongside the main thread (e.g., GC runs while the application runs).

When the main (user) thread terminates, the daemon thread is automatically terminated as well.

Non‑Daemon Thread

If the main thread ends, user threads keep running independently and are not affected.

Code Demonstration of Daemon vs. Non‑Daemon Threads

Creating a Daemon Thread

public static void main(String[] args) throws InterruptedException {
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("I am the child thread (user thread)");
            }
        }
    });
    // Must set daemon before starting the thread
    t1.setDaemon(true);
    t1.start();

    // Simulate main thread work
    for (int i = 0; i < 10; i++) {
        try { Thread.sleep(300); } catch (Exception e) { e.printStackTrace(); }
        System.out.println("main:i:" + i);
    }
    System.out.println("Main thread finished...");
}

The call to t1.setDaemon(true) must occur before t1.start() ; otherwise an IllegalThreadStateException is thrown.

Daemon Thread Output

main:i:0
main:i:1
main:i:2
I am the child thread (user thread)
main:i:3
main:i:4
main:i:5
I am the child thread (user thread)
main:i:6
main:i:7
main:i:8
I am the child thread (user thread)
main:i:9
Main thread finished...

After the main thread ends, the daemon thread stops automatically.

Non‑Daemon Thread Example

public static void main(String[] args) {
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            while (true) {
                try { Thread.sleep(1000); } catch (Exception e) { /* TODO */ }
                System.out.println("I am the child thread (user thread)");
            }
        }
    });
    // No setDaemon call – this is a user thread
    t1.start();

    for (int i = 0; i < 10; i++) {
        try { Thread.sleep(300); } catch (Exception e) { e.printStackTrace(); }
        System.out.println("main:i:" + i);
    }
    System.out.println("Main thread finished...");
}

Non‑Daemon Thread Output

main:i:0
main:i:1
main:i:2
I am the child thread (user thread)
main:i:3
main:i:4
main:i:5
I am the child thread (user thread)
main:i:6
main:i:7
main:i:8
I am the child thread (user thread)
main:i:9
Main thread finished...
I am the child thread (user thread)
I am the child thread (user thread)
... (continues indefinitely)

When the main thread finishes, the non‑daemon (user) thread continues to run, demonstrating that it is independent of the main thread's lifecycle.

JavaconcurrencyProgrammingThreadsDaemonThread
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.