The Threads which are executing in the background are called Daemon Threads
Ex: Garbage Collector, signal Dispatcher, Attach Listener etc...

The main objective of Daemon Threads is to provide support for non-Daemon Threads (main Thread).
For example, if main Thread runs with low memory, then JVM runs Garbage collector to destroy useless objects so that number of bytes of free memory will be improved with this free memory, Main Thread can continue its execution.

Usually, the daemon Threads having low priority but based on our requirement Daemon Threads can run with high priority also.

we can check Daemon nature of a Thread by using isDaemon() method of Thread class.

public boolean isDaemon()

we can change Daemon nature of a Thread by using setDaemon() method.

public void setDaemon(boolean b)

But, changing Daemon nature is possible before starting of a Thread only. After starting a Thread if we are trying to change Daemon nature then we will get runtime exception saying IllegalThreadStateException.

What is Default nature of a Thread?

By default, main thread is always non-Daemon and for all remaining Threads Daemon nature will be inherited from parent to child i.e., if the parent Thread is Daemon, then automatically child Thread is also Daemon and if parent Thread is non-Daemon, then automatically child Thread is also non-Daemon.


Note: It is impossible to change Daemon nature of main Thread because it is already started by JVM at beginning.


class MyThread extends Thread{
}
class DaemonTest{
    public static void main(String[] args){
        System.out.println(Thread.currentThread().isDaemon()); //false
        //Thread.currentThread().setDaemon(true); //RE:IllegalThreadStateException
        MyThread t = new MyThread();
        System.out.println(t.isDaemon()); //false
        t.setDaemon(true);
        System.out.println(t.isDaemon()); // true
    }
}

Whenever last non-Daemon Thread terminates automatically all Daemon Threads will be terminated irrespective of their positions.

class MyThread extends Thread{
    public void run(){
        for(int i = 0; i < 10; i++){
            System.out.println("Child Thread");
            try{
                Thread.sleep(2000);
            }
            catch(InterruptedException e){
            }
        }
    }
}
class DaemonThreadDemo{
    public static void main(String[] args){
        MyThread t = new MyThread();
        t.setDaemon(true); // line-1
        t.start();
        System.out.println("End of Main Thread");
    }
}

If we are commenting line-1 both main and child Threads are non-Daemon and Hence both Threads will be executed until their completion

If we are not commenting line-1 then main Thread is non-Daemon and child Thread is Daemon. Hence whenever Main Thread terminates automatically child Thread will be terminated.
In this case output is
End of Main Thread
Child Thread
(or)
Child Thread
(or)
Child Thread
End of Main Thread



Multithreading Models:


In java, Multithreading concept is implemented by using two models.
1. Green Thread model
2. Native OS model


Green Thread Model:


The Thread which is managed completely by JVM without taking underlying OS support is called Green Thread.
Very few operating systems like SUN Solaris provide support for Green Thread model.
Anyway, Green Thread model is deprecated and not recommended to use.


Native OS model:


The Thread which is managed by the JVM with the help of underlying OS, is called Native OS model
. All windows-based operating system provide support for Native OS model.

How to stop a Thread?
we can stop a Thread execution by using stop() method of thread class.


public void stop()

if we call stop() method then immediately the Thread will entered inti Dead state.
Anyway stop(0 method is deprecated and not recommended to use.

How to suspend and resume of a Thread?
we can suspend a Thread by using suspend() method of Thread class then immediately the Thread will be entered into suspended state

we can resume the suspended Thread by using resume() method on Thread class then suspended Thread can continue its execution.


public void suspend()
public void resume()

Anyway, these methods are deprecated and not recommended to use.
Thread LifeCycle:

ThreadLifeCycle