Daemon Thread in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depends on the mercy of user threads i.e. when all the user threads die, JVM terminates this thread automatically. There are many Daemon threads in java running automatically e.g. gc, finalizer, etc.

What is Daemon Thread in Java?

Daemon thread in Java is a low-priority thread that performs background operations such as garbage collection, finalizer, Action Listeners, Signal dispatches, etc.

Daemon thread in Java is also a service provider thread that helps the user thread. Its life is at the mercy of user threads; when all user threads expire, JVM immediately terminates this thread.

In simple words, we can say that it provides services to user threads for background-supporting tasks. Daemon thread in Java has no role in life other than to serve user threads.

When There is No User Thread, Why Does JVM Stop the Daemon Thread in Java?

The daemon thread's main purpose is to provide services to the user thread for background task support.

Why should JVM keep running this thread if there is no user thread?

As a result, if there is no user thread, JVM stops the daemon thread.

Properties of Daemon Thread in Java

  • It's a thread with the lowest priority possible.
  • They won't be able to stop the JVM from quitting once all of the user threads have completed their tasks.
  • When all user threads have completed their execution, the JVM terminates.
  • If JVM finds a running daemon thread, it terminates the thread and, after that, shuts it down.
  • The JVM is unconcerned about whether the Daemon thread is active or not.
  • The nature of a demon is passed down from parent to child. That is, if the parent is a Daemon, the child will be a Daemon as well, and if the parent is a non-daemon, the child will be a non-daemon as well.

Methods for Daemon Thread in Java by Thread Class

S.No.MethodDescription
1.public void setDaemon(boolean status)This method marks whether the current thread as a daemon thread or a user thread.
2.public final boolean isDaemon()This method is used to determine whether or not the current thread is a daemon. If the thread is Daemon, it returns true. Otherwise, false is returned.

Daemon Thread in Java Examples

  1. SetDaemon() and isDaemon() methods are demonstrated in below Java program.

Output:

  1. Exception in Daemon() Thread is demonstrated in the below Java program.

Runtime exception:

Output:

Exceptions in a Daemon Thread in Java

No.ExceptionsDescription
1IllegalThreadStateException.If you call the setDaemon() method after the thread has started, it will throw an exception.
2SecurityExceptionIf the current thread is unable to change this thread

Daemon Thread vs User Thread

With the aid of the table below, learn more about the distinctions between Daemon and User threads:

Daemon ThreadsUser Threads (Non-daemon)
Low Priority threadsHigh priority threads
The JVM does not wait for its execution to complete.The JVM waits till the execution is finished.
Life is dependent on user threadsLife is independent
Daemon threads are created by JVMAn application creates its own user threads.
provides service to the user thread which runs in the backgroundUsed for foreground tasks

Conclusion

We've seen what daemon threads in java are and how they may be utilised in a few real applications in this fast lesson.

  • The threads which are executing in the background are called daemon threads.
  • The main objective of daemon threadin Java is to provide support for non-daemon thread like main thread.
  • Whenever the program runs with low memory, the JVM will execute Garbage collector to provide free memory. So that the main thread can continue it's execution.
  • Daemon threads are Low Priority threads.
  • A thread that executes main logic of the project is called non-daemon thread.
  • Every user defined thread is created as non-daemon thread by default, because main thread is a non-daemon thread.