Daemon thread

A daemon thread is a java thread that runs in the background.
These threads are of low priority and are typically used for tasks that do not need to be run to completion.

Since they are not bound to be completed, they do not prevent the JVM from exiting. In other words, JVM can shut down even if a daemon thread is running.

Real life examples

Daemon threads are used for tasks which should run in the background.
Examples of daemon threads in java are

1. Garbage collector

When the JVM discovers that there are objects with null reference, it runs a garbage collector thread to wipe them out of heap memory.

2. Logger

A thread which logs messages to a file or console.

3. Monitoring

A thread which monitors the resources utilized by a system.

4. Watcher services

Threads which keep track of file creation, deletion and read and write operations in a directory.
5. Web servers use daemon threads to handle requests.
6. Databases use daemon threads to perform maintenance tasks.

Benefits

Daemon threads are useful due to below reasons:
1. They do not need to be monitored, which can free up resources.
2. They can be used for tasks that do not need to be run constantly.
3. They can be used to improve performance by running in the background.

Non-daemon threads

Non-daemon or User threads are threads that need to be run to completion, such as a thread that is running a program.
A user thread is a thread that is created by the application and is typically used to complete a task that is required for the application to function.
So, JVM cannot terminate when a user thread is executing.

Daemon thread example

A daemon thread in java is created using setDaemon() method on the thread object before starting it.
To check if a thread is daemon, use isDaemon() method on the same thread object. Example,

Thread myThread = new Thread();
myThread.setDaemon(true);
myThread.start();
myThread.isDaemon(); // true

JVM termination

As stated earlier, a daemon thread does not stop JVM from exiting.
This means if a daemon thread is still running, and JVM has finished execution, then it will not wait for the daemon thread to completed before exiting.
Below is an example to demonstrate this.

Thread myThread = new Thread(() -> {
  for (int i = 0; i < 5; i++) {
    System.out.println("Daemon thread "+i);
    try {
      // halt new thread
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }			
        }
});
myThread.setDaemon(true);
myThread.start();
// halt main thread
try {
  Thread.sleep(1000);
} catch (InterruptedException e) {
  e.printStackTrace();
}
System.out.println("JVM exiting");

Above example starts a new thread, which loops from 1 to 5 with a delay of 1 second between iterations.

After starting main thread, we make the main thread delay for 1 second using sleep() method.
So, now the daemon thread will take 5 seconds to execute, while the main thread will complete after 1 second.
Look at the output

Daemon thread 0
JVM exiting

This means that the daemon thread did not complete.
Comment out setDaemon(true) or set it to false.
Below will be the output

Daemon thread 0
JVM exiting
Daemon thread 1
Daemon thread 2
Daemon thread 3
Daemon thread 4

This shows that JVM does not wait for daemon thread to complete while it waits till the non-daemon or user thread finishes.

Hope the article was helpful.