These are those thread which execute in background in support of main Thread (creator of that Thread). Life of a Daemon Thread is up-to life of main Thread which start the dameon thread. This daemon thread have very low priority. If we want to make any thread to daemon thread, then don't start the thread hence otherwise it will throw IllegalThreadStateException.
public class Dameon_Thread extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){//checking for daemon thread
System.out.println("daemon thread work");
}
else{
System.out.println("user thread work");
}
}
public static void main(String[] args){
Dameon_Thread ob1=new Dameon_Thread(); //creating threads
Dameon_Thread ob2=new Dameon_Thread();
Dameon_Thread ob3=new Dameon_Thread();
ob1.setDaemon(true);//setting ob1 to daemon thread
ob1.start();//starting threads
ob2.start();
ob3.start();
}
}
0 Comment(s)