Posts

Showing posts with the label program to demonstrate the concept of daemon threads

Write a Java program to demonstrate the concept of daemon threads

Write a Java program to demonstrate the concept of daemon threads   program :  public class DaemonThread extends Thread  {  int i;  public void run()  {  for(i=0;i<5;i++);  {  System.out.println(this.getName()+" "+i);  }  }  public static void main(String[] args)  {  DaemonThread d=new DaemonThread();  DaemonThread d1=new DaemonThread();  d.setName("\nDAEMON THREAD");  d1.setName("NORMAL THREAD");  d.setPriority(Thread.MIN_PRIORITY);  d.start();  d1.start();  }  } Output : NORMAL THREAD 5 DAEMON THREAD 5 Or DAEMON THREAD 5 NORMAL THREAD 5 daemon threads are low-priority threads whose only role is to provide services to user threads. Daemon threads are useful for background supporting tasks such as garbage collection, releasing memory of unused objects and removing unwanted entries from the cache.