Wednesday, 18 November 2015

What is daemon threads? Explain briefly

Daemon thread : It 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.
Note:

 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.

Advantage: The main 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.
Methods: Thread class provides 2 methods for java daemon threads

S.No
Method
Description
1
public void setDaemon(boolean status)
It is used to mark the current thread as daemon thread or user thread
2
public boolean isDaemon()
It is used to chaeck that current is daemon
 Example:

 

No comments:

Post a Comment