Reference Link : Link

Donate : Link

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

  • They can not prevent the JVM from exiting when all the user threads finish their execution.
  • JVM terminates itself when all user threads finish their execution
  • If JVM finds running daemon thread, it terminates the thread and after that shutdown itself. JVM does not care whether Daemon thread is running or not.
  • It is an utmost low priority thread.

Methods :

1. void setDaemon(boolean status)

This method is used to mark the current thread as daemon thread or user thread. For example if I have a user thread tU then tU.setDaemon(true) would make it Daemon thread. On the other hand if I have a Daemon thread tD then by calling tD.setDaemon(false) would make it user thread.

Syntax :

public final void setDaemon(boolean on)

2. boolean isDaemon():

This method is used to check that current is daemon. It returns true if the thread is Daemon else it returns false.

Syntax :

public final boolean isDaemon()

package com.codeFactory;

Output :

Hello from Worker 0
Hello from Worker 1
Main Thread ending

Output :

d1 is Daemon Thread
d2 is User Thread