Daemon threads - 12 salient features of Daemon Thread in java


Contents of page :
  • Daemon threads >
  • 12 salient features of daemon() threads >
  • Program


Daemon threads >
Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.

12 salient features of daemon() threads >

  1. Thread scheduler schedules these threads only when CPU is idle.

  1. Daemon threads are service oriented threads, they serves all other threads.

  1. These threads are created before user threads are created and die after all other user threads dies.

  1. Priority of daemon threads is always 1 (i.e. MIN_PRIORITY).

  1. User created threads are non daemon threads.

  1. JVM can exit when only daemon threads exist in system.

  1. Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.

  1. we can use isDaemon() method to check whether thread is daemon thread or not.

  1. we can use setDaemon(boolean on) method to make any user method a daemon thread. Generally, We must not make user threads as daemon.

  1. If setDaemon(boolean on) is called on thread after calling start() method than IllegalThreadStateException is thrown.

  1. You may like to see how daemon threads work, for that you can use VisualVM or jStack. I have provided Thread dumps over there which shows daemon threads which were intermittently running in background.

Some of the daemon threads which intermittently run in background are >
"RMI TCP Connection(3)-10.175.2.71" daemon
"RMI TCP Connection(idle)" daemon
"RMI Scheduler(0)" daemon
"C2 CompilerThread1" daemon
"GC task thread#0 (ParallelGC)"



  1. Now, Lets create program >

Program to create daemon thread by using setDaemon() method and also use isDaemon() method to check whether thread is daemon or not.

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DaemonTest {
   public static void main(String[] args) throws InterruptedException {
  
          final Thread thread1=new Thread("Thread-1"){
                 public void run() {
                       System.out.println(Thread.currentThread().getName()+" has started");
                       System.out.println(Thread.currentThread().getName()+" has ended");
                 }
  
          };
          thread1.setDaemon(true);   //setting thread to daemon.
          System.out.println("is thread1 daemon thread : "
                                     +thread1.isDaemon());   //checking thread isDeamon ?
          thread1.start(); //start daemon thread
         
         
   }  
}
/*
is thread1 daemon thread : true
Thread-1 has started
Thread-1 has ended
*/


Program to show  > if setDaemon(boolean on) is called on thread after calling start() method than IllegalThreadStateException is thrown.

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DaemonTestException {
   public static void main(String[] args) throws InterruptedException {
  
          final Thread thread1=new Thread("Thread-1"){
                 public void run() {
                       System.out.println(Thread.currentThread().getName()+" has started");
                       System.out.println(Thread.currentThread().getName()+" has ended");
                 }
  
          };
          thread1.start(); //start daemon thread
          thread1.setDaemon(true);   //setting thread to daemon after start(), will throw IllegalThreadStateException.
         
   }  
}
/*
Thread-1 has started
Thread-1 has ended
Exception in thread "main" java.lang.IllegalThreadStateException
   at java.lang.Thread.setDaemon(Unknown Source)
   at DaemonTestException.main(DaemonTestException.java:13)
*/




RELATED LINKS>

Thread basics >

What is thread in java

Thread states/ Thread life cycle in java

Difference between Process and Thread in java

Implementing Threads in java by implementing Runnable interface and extending Thread class

Threads implement their own stack - demonstration using program and diagram

Differences between implementing Runnable interface and extending Thread class

Thread behaviour is unpredictable

When threads are not lightweight process in java, in fact they become heavy weight process


Important Thread methods (salient features, usage with programs)>

Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join

Sleep() method in threads - 10 key features with programs

Yield() method in threads - 8 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Using Suspend and resume method in threads



Differences between important thread methods >

Difference between wait() and sleep() method in threads

Differences and similarities between yield() and sleep() in threads

Difference between notify() and notifyAll() methods, with program

Difference between wait() and wait(long timeout) -What will be Thread states


No comments:

Post a Comment