Sunday, 27 November 2016

What is Daemon Thread In Java ?

Previously we have discussed about
Daemon Thread

When we create a Thread in java, by default its a user defined thread.In order to create a daemon thread ,thread class provide a method named as setDaemon() which is used to set Thread as Daemon thread.

Daemon thread is a low priority based thread that runs in background to perform some tasks such as : garbage collection etc.

Daemon thread life depends upon on the user defined threads.

Thread.setDaemon(true) : This method makes a User Thread to Daemon thread but it can only be called before starting a Thread in java.It will throw IllegalThreadStateException if corresponding Thread is already started and running.

Thread.setDaemon(false) : This method makes Daemon Thread back to normal thread .

public boolean isDaemon() : This method is used to determine weather a thread is Daemon or not.

Difference between Daemon and Non Daemon Thread :
  • JVM does not wait for any daemon thread to finish before exiting.
  • Daemon threads are treated differently than user defined thread when JVM terminates, finally blocks are not called ,and JVM just exits.

Example of Daemon Thread:

public class DeamonThreadExample extends Thread {
public void run(){
try {
if(Thread.currentThread().isDaemon()){
System.out.println("Daemon Thread Executing.");
}
else{
System.out.println("Normal Thread is Executing.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Daemon Thread Exiting.");
}
}
public static void main(String[] args) {
DeamonThreadExample t1 = new DeamonThreadExample();
DeamonThreadExample t2 = new DeamonThreadExample();
t1.setDaemon(true);
t1.start();
t2.start();
}

}
Program Output:
Daemon Thread Executing.
Normal Thread is Executing.
Daemon Thread Exiting.
Daemon Thread Exiting.

Example of Daemon Thread throwing Exception:

public class DeamonThreadExample extends Thread {
public void run(){
try {
if(Thread.currentThread().isDaemon()){
System.out.println("Daemon Thread Executing.");
}
else{
System.out.println("Normal Thread is Executing.");
}
catch (Exception e) {
e.printStackTrace();
finally {
System.out.println("Daemon Thread Exiting.");
}
}
public static void main(String[] args) {
DeamonThreadExample t1 = new DeamonThreadExample();
DeamonThreadExample t2 = new DeamonThreadExample();
t1.setDaemon(true);
t1.start();
t2.start();
}

}
Program Output:
Normal Thread is Executing.Exception in thread "main" 
Daemon Thread Exiting.
java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Unknown Source)
at thread.DeamonThreadExample.main(DeamonThreadExample.java:23)



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment