AdSense

AdSense3

Wednesday, 1 October 2014

Daemon Thread in Java

Daemon thread in java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.

There are many java daemon threads running automatically e.g. gc, finalizer etc.
You can see all the detail by typing the jconsole in the command prompt. The jconsole tool provides information about the loaded classes, memory usage, running threads etc.

Points to remember for Daemon Thread in Java

  • It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.
  • Its life depends on user threads.
  • It is a low priority thread.

Why JVM terminates the daemon thread if there is no user thread?

The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread.

Methods for Java Daemon thread by Thread class

The java.lang.Thread class provides two methods for java daemon thread.
No.MethodDescription
1)public void setDaemon(boolean status)is used to mark the current thread as daemon thread or user thread.
2)public boolean isDaemon()is used to check that current is daemon.

Simple example of Daemon thread in java

File: MyThread.java
  1. public class TestDaemonThread1 extends Thread{  
  2.  public void run(){  
  3.   if(Thread.currentThread().isDaemon()){//checking for daemon thread  
  4.    System.out.println("daemon thread work");  
  5.   }  
  6.   else{  
  7.   System.out.println("user thread work");  
  8.  }  
  9.  }  
  10.  public static void main(String[] args){  
  11.   TestDaemonThread1 t1=new TestDaemonThread1();//creating thread  
  12.   TestDaemonThread1 t2=new TestDaemonThread1();  
  13.   TestDaemonThread1 t3=new TestDaemonThread1();  
  14.   
  15.   t1.setDaemon(true);//now t1 is daemon thread  
  16.     
  17.   t1.start();//starting threads  
  18.   t2.start();  
  19.   t3.start();  
  20.  }  
  21. }  

Output

daemon thread work
user thread work
user thread work

Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.

File: MyThread.java
  1. class TestDaemonThread2 extends Thread{  
  2.  public void run(){  
  3.   System.out.println("Name: "+Thread.currentThread().getName());  
  4.   System.out.println("Daemon: "+Thread.currentThread().isDaemon());  
  5.  }  
  6.   
  7.  public static void main(String[] args){  
  8.   TestDaemonThread2 t1=new TestDaemonThread2();  
  9.   TestDaemonThread2 t2=new TestDaemonThread2();  
  10.   t1.start();  
  11.   t1.setDaemon(true);//will throw exception here  
  12.   t2.start();  
  13.  }  
  14. }  
Output:exception in thread main: java.lang.IllegalThreadStateException

No comments:

Post a Comment