What is a daemon thread in Java?


A daemon thread in java is the one which executes continuously in the background. You can set a thread daemon using the method setDaemon().

Example

Live Demo

class adminThread extends Thread {
   adminThread() {
      setDaemon(true);
   }
   public void run() {
      boolean d = isDaemon();
      System.out.println("daemon = " + d);
   }
}  
public class ThreadDemo {
   public static void main(String[] args) throws Exception {    
      Thread thread = new adminThread();
      System.out.println("thread = " + thread.currentThread());
      thread.setDaemon(true);
      thread.start();
   }
}

Output

thread = Thread[main, 5, main]
raja
Published on 23-Apr-2018 01:40:05
Advertisements