In java we have two type of Threads : Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our program code. JVM doesn't terminates unless all the user thread terminate.
On the other hand we have Daemon threads.Typically these threads are service provider threads. When the thread that created the daemon thread ends, the daemon thread dies with it.This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.A live daemon thread does not prevent an application from exiting.An application exits when there are no non-daemon threads running.
A daemon thread should be used for some background task that might provide a service to the applications. e.g. a Server thread listening on a port for the clients` requests ,Java Garbage Collector.Garbage collector runs as a daemon thread to reclaim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.
When a new thread is created it inherits the daemon status of its parent. You can explicitly specify a thread created by a user thread to be a daemon thread by calling setDaemon(true) on a Thread object.Note that the setDaemon() method must be called before the thread's start() method is invoked.Once a thread has started executing (i.e., its start() method has been called) its daemon status cannot be changed. To determine if a thread is a daemon thread, use the accessor method isDaemon().
Summary
The difference between these two types of threads is straightforward: If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.
Daemon Thread Example
class MyThread extends Thread {
Thread parent;
MyThread(Thread parent) {
this.parent = parent;
}
public void run() {
System.out.println("is mythread daemon=" + isDaemon());
while (true) {
System.out.println("mythread running...Is Parent Alive="
+ parent.isAlive());
}
}
}
public class Main {
public static void main(String args[]) throws InterruptedException {
System.out.println("Starting main method");
MyThread t1 = new MyThread(Thread.currentThread());
t1.setDaemon(true);
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leaving main method");
}
}
The output is given below
Starting main method
is mythread daemon=true
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
.
.
.
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
Leaving main method
You can see that daemon thread,mythread stops execution just after the parent user thread, main has finished its execution.
User Thread Example
class MyThread extends Thread {
Thread parent;
MyThread(Thread parent) {
this.parent = parent;
}
public void run() {
System.out.println("is mythread daemon=" + isDaemon());
while (true) {
System.out.println("mythread running...Is Parent Alive="
+ parent.isAlive());
}
}
}
public class Main {
public static void main(String args[]) throws InterruptedException {
System.out.println("Starting main method");
MyThread t1 = new MyThread(Thread.currentThread());
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leaving main method");
}
}
The output is given below
Starting main method
is mythread daemon=false
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
.
.
.
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
Leaving main method
mythread running...Is Parent Alive=false
mythread running...Is Parent Alive=false
.
.
.
You can see that non-daemon thread,mythread is running even after the main thread has finished its execution.
On the other hand we have Daemon threads.Typically these threads are service provider threads. When the thread that created the daemon thread ends, the daemon thread dies with it.This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.A live daemon thread does not prevent an application from exiting.An application exits when there are no non-daemon threads running.
A daemon thread should be used for some background task that might provide a service to the applications. e.g. a Server thread listening on a port for the clients` requests ,Java Garbage Collector.Garbage collector runs as a daemon thread to reclaim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.
When a new thread is created it inherits the daemon status of its parent. You can explicitly specify a thread created by a user thread to be a daemon thread by calling setDaemon(true) on a Thread object.Note that the setDaemon() method must be called before the thread's start() method is invoked.Once a thread has started executing (i.e., its start() method has been called) its daemon status cannot be changed. To determine if a thread is a daemon thread, use the accessor method isDaemon().
Summary
The difference between these two types of threads is straightforward: If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.
Daemon Thread Example
class MyThread extends Thread {
Thread parent;
MyThread(Thread parent) {
this.parent = parent;
}
public void run() {
System.out.println("is mythread daemon=" + isDaemon());
while (true) {
System.out.println("mythread running...Is Parent Alive="
+ parent.isAlive());
}
}
}
public class Main {
public static void main(String args[]) throws InterruptedException {
System.out.println("Starting main method");
MyThread t1 = new MyThread(Thread.currentThread());
t1.setDaemon(true);
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leaving main method");
}
}
The output is given below
Starting main method
is mythread daemon=true
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
.
.
.
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
Leaving main method
You can see that daemon thread,mythread stops execution just after the parent user thread, main has finished its execution.
User Thread Example
class MyThread extends Thread {
Thread parent;
MyThread(Thread parent) {
this.parent = parent;
}
public void run() {
System.out.println("is mythread daemon=" + isDaemon());
while (true) {
System.out.println("mythread running...Is Parent Alive="
+ parent.isAlive());
}
}
}
public class Main {
public static void main(String args[]) throws InterruptedException {
System.out.println("Starting main method");
MyThread t1 = new MyThread(Thread.currentThread());
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leaving main method");
}
}
The output is given below
Starting main method
is mythread daemon=false
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
.
.
.
mythread running...Is Parent Alive=true
mythread running...Is Parent Alive=true
Leaving main method
mythread running...Is Parent Alive=false
mythread running...Is Parent Alive=false
.
.
.
You can see that non-daemon thread,mythread is running even after the main thread has finished its execution.
No comments:
Post a Comment