Daemon threads are special threads in Java. For easier understanding, you may also call them as background jobs. Since they don’t have direct interaction with the user, these threads are of lower priority than the normal non-daemon threads. As with any thread, daemon threads can also be JVM or user initiated. For example, garbage collection thread is a JVM initiated daemon thread.

In this tutorial we shall see how to create a user thread and mark it as daemon. By doing so we are automatically reducing the priority of the thread. The source code example to demonstrate the working of daemon thread is:

Example code for daemon threads

package com.example;

public class Test implements Runnable{
	public static void main(String args[]) {
		Test t = new Test();
		Thread t1 = new Thread(t);
		t1.setDaemon(true);
		System.out.println("is daemon : " + t1.isDaemon());
		t1.start();
		
	}
	
	public void run() {
		System.out.println("abc");
		try {
			Thread.currentThread().sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("def");
	}
}

Output:

is daemon : true
abc

Important points to note about daemon threads in Java

1) They are of lower priority that other threads.
2) The JVM automatically shuts down when there is nothing but daemon threads to execute.
3) In a Java program, developers should only mark very low priority jobs to daemon threads.
4) There are API methods given in the java.lang.Thread class by using which we can mark a thread as daemon and also check if a specific thread is daemon or not.
5) Daemon threads in Java got their name from daemon process in UNIX which run in the background forever.

Daemon threads in Java with example code admin Core Java
Daemon threads are special threads in Java. For easier understanding, you may also call them as background jobs. Since they don't have direct interaction with the user, these threads are of lower priority than the normal non-daemon threads. As with any thread, daemon threads can also be JVM or...
Daemon threads are special threads in Java. For easier understanding, you may also call them as background jobs. Since they don't have direct interaction with the user, these threads are of lower priority than the normal non-daemon threads. As with any thread, daemon threads can also be JVM or user initiated. For example, garbage collection thread is a JVM initiated daemon thread. In this tutorial we shall see how to create a <a href="http://www.javaexperience.com/create-thread-using-runnable-interface-or-thread-class-in-java/" title="Create thread using Runnable interface or Thread class in Java">user thread</a> and mark it as daemon. By doing so we are automatically reducing the priority of the thread. The source code example to demonstrate the working of daemon thread is: <h2>Example code for daemon threads</h2> 1 Output: <blockquote> is daemon : true abc </blockquote> <h2>Important points to note about daemon threads in Java</h2> 1) They are of lower priority that other threads. 2) The JVM automatically shuts down when there is nothing but daemon threads to execute. 3) In a Java program, developers should only mark very low priority jobs to daemon threads. 4) There are API methods given in the java.lang.Thread class by using which we can mark a thread as daemon and also check if a specific thread is daemon or not. 5) Daemon threads in Java got their name from daemon process in UNIX which run in the background forever.