Home » Java programming language

How to create daemon thread in java?

Learn: How to create daemon thread in java? Can we make daemon thread as non daemon? Why daemon thread is required in java?
Submitted by Preeti Jain, on February 07, 2018

Daemon Thread in java

Daemon is a thread which executes in the background. Garbage Collector is an example of daemon thread as we have seen garbage collector runs in the background.

Purpose of daemon thread is to provide support for user thread.

For Example, if user thread is executing with low memory (i.e. user thread need more memory to execute) then jvm will call garbage collector(daemon thread) to destroy useless objects by memory space will get free and user thread can be executed quickly.

Usually, Threads having high priority will get a chance first to execute but daemon threads run with the lowest priority(but it does not mean that daemon thread can't run with highest priority it can if required).

There are few methods related to Daemon Thread

  1. public boolean isDaemon()
  2. public void setDaemon(boolean b)

In the above methods described as :

  • isDaemon() method checks whether thread is daemon or not.
  • setDaemon(boolean b) method makes daemon nature as non-daemon or non-daemon as a daemon if we required. by passing the value in method true or false. if we set true it makes non-daemon as daemon otherwise daemon as non-daemon.
  • setDaemon(boolean b) works fine before starting of a thread otherwise we will get a runtime exception.

Nature of Main thread is non-daemon and we can't change the behavior of main thread and other threads nature is inherited by parent or we can set by setDaemon(boolean b).


In this example, we are demonstrating the behavior of setDaemon(boolean b) method​​

class DaemonThread extends Thread{

	public void run(){
		System.out.println("This thread is a daemon thread" + Thread.currentThread().isDaemon());

class NDThread{
	public static void main(String[] args){
		DaemonThread dt = new DaemonThread();
		System.out.println("Behaviour before setDaemon()" + dt.isDaemon());
		System.out.println("Behaviour after setDaemon()" + dt.isDaemon());


D:\Java Articles>java NDThread
Behaviour before setDaemon()false
Behaviour after setDaemon()true
This thread is a daemon threadtrue


  1. Explain life cycle of a thread in java.
  2. How to Create Java Threads (java examples to create threads)?
  3. Thread Synchronisation in Java with Example.
  4. Java program to demonstrate example of thread.
  5. Java program to Join Threads.


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.