Java program on Daemon thread

Daemon thread is a low priority thread(for JVM) that runs in background to perform tasks such as garbage collection.
Difference between Daemon thread and user(or normal or non-deamon) threads is that the JVM does not wait for Daemon thread before exiting while it waits for user threads, it does not exit until unless all the user threads finish their execution.

PROGRAM:

class MyThread extends Thread {
    public void run() {
        if (Thread.currentThread().isDaemon())
            System.out.println("Deamon thread is running");
        else
            System.out.println("Normal thread is running");

    }
}

class DeamonDemo {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        MyThread td = new MyThread();
        td.setDaemon(true);
        t.start();
        td.start();
    }
}

OUTPUT: 

Normal thread is running
Deamon thread is running

Popular Posts