What’s Daemon thread in Java and Distinction to Non daemon thread

Disclosure: This text might comprise affiliate hyperlinks. If you buy, we might earn a small fee.

Daemon thread in Java is these thread that runs within the background and is usually created by JVM for performing background duties like Rubbish assortment and different housekeeping duties. The distinction between Daemon and Non-Daemon (Consumer Threads)  can be an attention-grabbing multi-threading interview query, which is requested totally on brisker stage java interviews. In a single line fundamental distinction between daemon thread and person thread is that as quickly as all person threads end execution java program or JVM terminates itself, JVM would not look forward to daemon thread to complete their execution.

As quickly because the final non-daemon thread is completed JVM terminates regardless of what number of Daemon threads exist or working inside JVM. On this java thread tutorial, we’ll see examples of Daemon threads in Java and a few extra variations between Daemon and non-daemon threads.

What is Daemon thread in Java and Difference to Non daemon thread - Tutorial Example

Vital factors about Daemon threads in Java

1. Any thread created by the primary thread, which runs the fundamental technique in Java is by default non-daemon as a result of Thread inherits its daemon nature from the Thread which creates it i.e. guardian Thread and because the fundamental thread is a non-daemon thread, some other thread created from it would stay non-daemon till explicitly made daemon by calling setDaemon(true).

2. Thread.setDaemon(true) makes a Thread daemon however it could solely be referred to as earlier than beginning Thread in Java. It would throw IllegalThreadStateException if the corresponding Thread is already began and working.

3. Daemon Threads are appropriate for doing background jobs like housekeeping, Although I’ve but to make use of it for any sensible objective in software code. tell us if in case you have used daemon thread in your java software for any sensible objective.

Distinction between Daemon and Non-Daemon thread in Java

listed here are a few variations between daemon and person thread in Java:

1) JVM would not wait for any daemon thread to complete earlier than current.

2) Daemon Thread is handled in another way than Consumer Thread when JVM terminates, lastly, blocks usually are not referred to as, Stacks usually are not unwounded and JVM simply exits.

And, when you want extra variations, here’s a good desk which exhibits all of the distinction between a daemon thread and a traditional thread in Java:

Programmer's Academy

Daemon Thread Instance in Java

Here’s a code instance of a daemon thread in java. we make a person thread daemon by calling setDaemon(true) and each time you run you will note a variable variety of print statements associated to “daemon thread is working” you’ll by no means see print assertion written in lastly block as a result of lastly is not going to be referred to as.

public class DaemonThreadExample {

    public static void fundamental(String args[]){
       Thread daemonThread = new Thread(new Runnable(){
           public void run(){
                   System.out.println(“Daemon thread is working”);
               }catch(Exception e){
                   System.out.println(“Daemon Thread exiting”); //by no means referred to as
       }, “Daemon-Thread”);
       daemonThread.setDaemon(true); //making this thread daemon


Daemon thread is working

Daemon thread is working

Daemon thread is working

Daemon thread is working

Daemon thread is working

Daemon thread is working

Daemon thread is working

Daemon thread is working

That’s all on What’s Daemon Thread in Java and the distinction between Daemon and non-daemon thread in Java with code instance of Daemon thread in Java. JVM additionally makes use of daemon thread for Rubbish assortment.

Different Java tutorial on Threads you might like

(Visited 9 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x