Thursday, April 25, 2024
HomeJavaWhat's Daemon thread in Java and Distinction to Non daemon thread

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


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

Daemon thread in Java is these thread that runs within the background and is generally created by JVM for performing background duties like Rubbish assortment and different housekeeping duties. The distinction between Daemon and Non-Daemon (Consumer Threads)  can also be an attention-grabbing multi-threading interview query, which is requested totally on more energizing degree java interviews. In a single line primary 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 ahead 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

Necessary factors about Daemon threads in Java

1. Any thread created by the primary thread, which runs the primary 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. father or mother Thread and because the primary thread is a non-daemon thread, another thread created from it is going to stay non-daemon till explicitly made daemon by calling setDaemon(true).

2. Thread.setDaemon(true) makes a Thread daemon however it could actually solely be referred to as earlier than beginning Thread in Java. It should 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 goal in utility code. tell us if in case you have used daemon thread in your java utility for any sensible goal.

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 a different 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, in case you want extra variations, here’s a good desk which reveals all of the distinction between a daemon thread and a standard thread in Java:

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 primary(String args[]){
   
       Thread daemonThread = new Thread(new Runnable(){
            @Override
           public void run(){
               attempt{
               whereas(true){
                   System.out.println(“Daemon thread is working”);
               }
                 
               }catch(Exception e){
                 
               }lastly{
                   System.out.println(“Daemon Thread exiting”); //by no means referred to as
               }
           }
       }, “Daemon-Thread”);
     
       daemonThread.setDaemon(true); //making this thread daemon
       daemonThread.begin();
     
     
}

Output:

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 could like



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments