Daemon threads are low-priority threads whose purpose is to provide services to user threads.
Daemon threads are only required while user threads are operating. They will not prevent the JVM from quitting after all user threads complete their execution.
Infinite loops, common in daemon threads, do not cause issues since no code, including final blocks, will be executed until all user threads have completed their execution. As a result, daemon threads should not be used for I/O activities.
The isDaemon() method of the Thread class is used to check if a given thread is a daemon thread or not.
public final boolean isDaemon()
The method has no parameters.
The method returns true if the current thread is a daemon thread. Otherwise, it returns false.
public class Main{public static void main(String[] args) throws InterruptedException {Runnable runnable = () -> {if(Thread.currentThread().isDaemon()) System.out.println(Thread.currentThread().getName() + " is a daemon thread");else System.out.println(Thread.currentThread().getName() + " is not a daemon thread");};Thread daemonThread = new Thread(runnable);daemonThread.setName("daemon-thread");daemonThread.setDaemon(true);Thread notDaemonThread = new Thread(runnable);notDaemonThread.setName("not-daemon-thread");daemonThread.start();notDaemonThread.start();Thread.sleep(1000);}}
Runnable interface, that is, a runnable that prints whether the current thread is a daemon thread or not with the help of the isDaemon() method.runnable called daemonThread.daemonThread thread as daemon-thread.daemonThread thread a daemon thread using the setDaemon() method.runnable called notDaemonThread.notDaemonThread thread as "not-daemon-thread".daemonThread and notDaemonThread using the start() method.