Categories: Java

Daemon thread in Java

A daemon thread in Java is a background thread with low priority that runs operations like garbage collection. A service provider thread in Java that offers services to the user thread is known as a daemon thread. Its survival is at the mercy of user threads; consequently, the JVM automatically kills this thread when all the user threads have expired.

Example:
Numerous Java daemon threads, including gc and finalizer, are automatically running.

Note:

  • The main thread is always non-daemon by default, but all other threads will inherit their daemon nature from their parents.
  • In other terms, if a parent is a daemon, the child will also be a daemon, and if a non-daemon, the child will also be a non-daemon.
Java Daemon thread methods by Thread class:

1. void setDaemon(boolean status): is used to specify whether the active thread is a user thread or a daemon thread.

Syntax:

public final void setDaemon(boolean on)

2. boolean isDaemon(): to affirm that current is daemon. This method returns true if this thread is a daemon thread; false otherwise

Syntax:

public final boolean isDaemon()
Example:
// Java program to demonstrate the usage of
// setDaemon() and isDaemon() method.

public class Coderz extends Thread
{
  public Coderz(String name){
    super(name);
  }

  public void run()
  {
    // Checking whether the thread is Daemon or not
    if(Thread.currentThread().isDaemon())
    {
      System.out.println(getName() + " is Daemon thread");
    }
    
    else
    {
      System.out.println(getName() + " is User thread");
    }
  }
  
  public static void main(String[] args)
  {
  
    Coderz th1 = new Coderz("th1");
    Coderz th2 = new Coderz("th2");
    Coderz th3 = new Coderz("th3");
  
    // Setting user thread t1 to Daemon
    th1.setDaemon(true);
      
    // starting first 2 threads
    th1.start();
    th2.start();

    // Setting user thread t3 to Daemon
    th3.setDaemon(true);
    th3.start();    
  }
}
Output:
th1 is Daemon thread
th2 is User thread
th3 is Daemon thread
Example: Exception in a Daemon thread
// Java program to demonstrate the usage of
// exception in Daemon() Thread

public class Coderz extends Thread
{
  public void run()
  {
    System.out.println("Thread name: " + Thread.currentThread().getName());
    System.out.println("DaemonThread : "
            + Thread.currentThread().isDaemon());
  }

  public static void main(String[] args)
  {
    Coderz th1 = new Coderz();
    Coderz th2 = new Coderz();
    th1.start();
    
    // Exception as the thread is already started
    th1.setDaemon(true);
    
    th2.start();
  }
}
Output:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1406)at Coderz.main(Coderz.java:20)
Thread name: Thread-0
DaemonThread : false

IllegalThreadStateException is thrown if the setDaemon() method is called after the thread has started.

Note: also read about the Java Thread Priority

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago