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:
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()
// 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();
}
}
th1 is Daemon thread
th2 is User thread
th3 is 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();
}
}
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…