In java any thread can be a Daemon thread. Basically daemon thread are those that works in background to support or to provide services for users threads running in same process.
If there are no any user threads available while daemon threads are running then jvm exist.Since daemon thread runs in background.
Example :
public class Ex extends Thread{
public void run() {
if (Thread.currentThread().isDaemon()) {
System.out.println("Daemon thread....");
}else{
System.out.println("worker thread....");
}
}
public static void main(String[] args){
Ex t1 = new Ex();
Ex t2 = new Ex();
Ex t3 = new Ex();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}
Output :
worker thread....
worker thread....
Daemon thread....
0 Comment(s)