08 January 2021

Daemon thread

  • 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
  • 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.
  • The user thread can be made ad daemon thread before calling start() function else it will  throw illegalThreadStateException.
  • A daemon thread runs in background and doesn’t prevent JVM from terminating. 
  • When there are no user threads running, JVM shutdown the program and quits. 
  • A child thread created from daemon thread is also a daemon thread.

Methods:

  • public void setDaemon(boolean status)
  • publicbooleanisDaemon()


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.

No comments:

Post a Comment

Most views on this month