Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Multithreading. No Comments on Daemon Threads
5
(1)

In this article we will learn what are daemon threads? and how to make thread as daemon thread along with example Java program.

 

A daemon thread is a thread which runs in the background. Example for daemon thread in Java is the garbage collector. In Java, thread can be divided into two categories:

  1. User threads
  2. Daemon threads

 

A user thread is a general thread which is created by the user. A daemon thread is also a user thread which is made as a daemon thread (background thread).

 

Difference between a user thread and a daemon thread is, a Java program will not terminate if there is at least one user thread in execution. But, a Java program can terminate if there are one or more daemon threads in execution.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

To work with daemon threads, Java provides two methods:

 

void setDaemon(boolean flag)

boolean isDaemon()

 

The setDaemon() method is used to convert a user thread into a daemon thread. The isDaemon() thread can be used to know whether a thread is a daemon thread or not.

 

Below program demonstrates a daemon thread:

class ChildThread implements Runnable
{
	Thread t;
	ChildThread(String name)
	{
		t = new Thread(this, name);
		t.setDaemon(true);
		t.start();
	}
	public void run()
	{
		try
		{
			while(true)
			{
				System.out.println("This thread is a daemon thread: "+t.isDaemon());
				System.out.println(t.getName()+": Hi");
				Thread.sleep(1000);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println("Child thread is interrupted");
		}
	}
}
class DaemonThread
{
	public static void main(String args[])
	{
		ChildThread one = new ChildThread("Daemon Thread");
	}
}

 

Output of the above program is:

This thread is a daemon thread: true

Daemon Thread: Hi

Daemon Thread: Hi

Daemon Thread: Hi

Daemon Thread: Hi

…

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *