Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Daemon-Thread-In-Java

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection. There are so many java daemon threads running automatically e.g. gc, finalizer etc.

  • • When a new thread is created it inherits the daemon status of its parent.
  • • Normal thread and daemon threads differ in what happens when they exit. When the JVM halts any remaining daemon threads are abandoned.
  • • finally blocks are not executed.
  • • stacks are not unwound - the JVM just exits.

Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.
Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.

setDaemon(true/false):

This method is used to specify that a thread is daemon thread.

public boolean isDaemon():

This method is used to determine the thread is daemon thread or not.Some more points that we have mentioned:

  • • Daemon threads shut down any time in between their flow, Non-daemon i.e. user thread executes completely.
  • • Daemon threads executes on low priority.
  • • Daemon threads are threads that run intermittently in background as long as other non-daemon threads are running.
  • • When all of the non-daemon threads complete, daemon threads terminates automatically.
  • • Daemon threads are service providers for non-threads running in the same process.
  • • JVM do not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is crated by us.
  • • Daemon threads acts as services in Windows.

Let's see an example of a daemon thread;

public class DaemonThread extends Thread
{
	public void run()
	{
		System.out.println("Entering run method");
		try
		{
			System.out.println("In run Method: currentThread() is" + Thread.currentThread());
			while (true)
			{
				try
				{
					Thread.sleep(500);
				} 
				catch (InterruptedException x) 
				{}
				System.out.println("In run method: woke up again");
			}
		} 
		finally {
			System.out.println("Leaving run Method");
		}
	}
	public static void main(String[] args) 
	{
		System.out.println("Entering main Method");
		DaemonThread t = new DaemonThread();
		t.setDaemon(true);
		t.start();
		try 
		{
			Thread.sleep(3000);
		} 
		catch (InterruptedException x) 
		{}
		System.out.println("Leaving main method");
	}
}

Output

Entering main Method
Entering run method
In run Method: currentThread() isThread[Thread-0,5,main]
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
Leaving main method

Note:If we want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.

For example:
class DaemonThreadDemo1 extends Thread
{
	public void run()
	{
		System.out.println("Name: "+Thread.currentThread().getName());
		System.out.println("Daemon: "+Thread.currentThread().isDaemon());
	}
	public static void main(String[] args)
	{
		DaemonThreadDemo1 t1=new DaemonThreadDemo1();
		DaemonThreadDemo1 t2=new DaemonThreadDemo1();
		t1.start();
		t1.setDaemon(true);
		t2.start();
	}
}

output

Exception in thread "main" java.lang.IllegalThreadStateException

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext