What is daemon thread? When would you use daemon thread?

Answer:

A daemon thread in Java is a thread that runs in the background within same process. Daemon threads are like Service providers for other threads running in the same process. Daemon threads are used for background support task like handling request or events etc. and are only needed while normal threads are running. Daemon threads work with normal/user threads.

Make a point that daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread.

Notes:

We need to understand two important Java APIs setDaemon() and isDaemon() while working with Daemon threads.

setDaemon(boolean value)- Set it to “true” to make thread as a daemon thread.

isDaemon()- to test if particular thread is Daemon.

Related Posts