Daemon Thread in Java

Category: Java   Tags: Java, Java Thread, Daemon Thread

Daemon threads are low priority background threads which provide services to user threads. It performs background tasks e.g. finalizer, garbage collection etc.

User threads are high priority threads which always run in foreground.

If there are no user threads, JVM will exit even if daemon threads are running. Following two methods are used to set and get a daemon thread in Java:

public void setDaemon(boolean status)
If status is true, thread will be marked as a daemon thread else it will be a user thread only.
public boolean isDaemon()
Check if a thread is daemon or not. Returns true if it is a daemon thread.

Here is an example of daemon thread in Java:

DaemonThreadExample.java

                            package com.tutorial.javabasic;

                            class MyThread implements Runnable {

                                @Override
                                public void run() {
                                    boolean daemon = Thread.currentThread().isDaemon();
                                    System.out.println(Thread.currentThread().getName() + " is Daemon Thread: " + daemon);
                                    System.out.println(Thread.currentThread().getName() + " is User Thread: " + !daemon);
                                }
                            }

                            class DaemonThreadExample {

                                public static void main(String s[]) {
                                    MyThread myThread = new MyThread();
                                    Thread thread1 = new Thread(myThread, "Thread1");
                                    Thread thread2 = new Thread(myThread, "Thread2");
                                    Thread thread3 = new Thread(myThread, "Thread3");

                                    thread3.setDaemon(true);

                                    thread1.start();
                                    thread2.start();
                                    thread3.start();
                                }
                            }
                        

Output:

                            Thread1 is Daemon Thread: false
                            Thread1 is User Thread: true
                            Thread3 is Daemon Thread: true
                            Thread2 is Daemon Thread: false
                            Thread3 is User Thread: false
                            Thread2 is User Thread: true
                          

Once a thread started you can not convert it into a daemon thread. For example do the below changes in the above code:

                            thread1.start();
                            thread2.start();
                            thread3.start();

                            thread3.setDaemon(true);
                          

You will get exception:

                            Thread1 is Daemon Thread: false
                            Thread1 is User Thread: true
                            Thread3 is Daemon Thread: false
                            Thread3 is User Thread: true
                            Thread2 is Daemon Thread: false
                            Thread2 is User Thread: true
                            Exception in thread "main" java.lang.IllegalThreadStateException
                                at java.lang.Thread.setDaemon(Thread.java:1352)
                                at com.tutorial.javabasic.DaemonThreadExample.main(DaemonThreadExample.java:27)
                          

User Threads Vs Daemon Threads In Java

User Thread Daemon Thread
User threads are foreground threads. Daemon threads are background threads.
These are high priority threads. These are low priority threads.
User threads are created for some specific task. Daemon Threads created to support user thread.
It will be terminated by themselves. Daemon threads will be terminated if all user threads has finished their tasks.
User threads are created by users. Daemon threads created by JVM.