Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

Difference between Daemon threads and user threads in Java

Ravi

Daemon threads

Daemon threads are low-priority threads whose purpose is to provide services to user threads.

User threads

User threads are high-priority threads whose purpose is to perform complex tasks that may or may not be supported by Daemon threads.

Daemon Threads User Threads
JVM never waits until all the user threads have completed their work. It exits as soon as all user threads have finished their work. JVM never exits until all the user threads have completed their work.
The lifecycle of the Daemon threads is dependent on the user threads. The lifecycle of user threads is independent.
JVM force stops/terminates the Daemon threads. JVM does not force stop/terminate the user threads.
Daemon threads have a lower priority compared to user threads. User threads have a higher priority compared to Daemon threads.
Daemon threads are service providers for user threads running in the same process. User threads can use daemon thread services but are not entirely dependent on them.

Example

public class Main{

    public static void main(String[] args) throws InterruptedException {
        Runnable daemonRunnable = () -> {
            while(true){
                System.out.println("I'm a daemon thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable userRunnable = () -> {
            try {
                System.out.println("I'm a user thread");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread daemonThread = new Thread(daemonRunnable);
        daemonThread.setName("daemon-thread");
        daemonThread.setDaemon(true);
        Thread notDaemonThread = new Thread(userRunnable);
        notDaemonThread.setName("not-daemon-thread");
        daemonThread.start();
        notDaemonThread.start();
        Thread.sleep(4000);
    }
}
Try it out!

Explanation

  • Lines 4–13: We define a runnable called daemonRunnable to run for the Daemon thread. We use a while loop in the runnable function to print a string and sleep for one second.
  • Lines 15–22: We define a runnable called userRunnable to run for the user thread. In the runnable function, we print a string and sleep for three seconds.
  • Lines 24–31: We create a Daemon and a user thread with the corresponding runnable.

After executing the code, we can observe that the Daemon thread is still in the running state, and prints “I’m a daemon thread,” while the user thread has completed running the runnable. Although the daemon thread is still running, the JVM exits.

Note: For more on Daemon threads in Java, refer to: How to create a daemon thread in Java and How to check if a thread is a daemon thread.

RELATED TAGS

java
RELATED COURSES

View all Courses

Keep Exploring