Java Program on Daemon Thread

Free Java courses with 37 real-time projects - Learn Java

Program 1

package dataflair;

public class TestMain 
{
    public static void main(String[] args) throws InterruptedException
    {
        System.out.println("This is main Thread...");    
        System.out.println("Main Thread Status: "+Thread.currentThread().isDaemon());
        MyThread mt1=new MyThread();
        mt1.setDaemon(true);  
        mt1.start();
        System.out.println("Child Thread Status: "+mt1.isDaemon());
        System.out.println("Main Thread Status: "+Thread.currentThread().isDaemon());
        Thread.sleep(10000);
      
    }
    
}

Program 2

package dataflair;

public class MyThread extends Thread
{
    public void run()
    {
      try
      {      
        System.out.println("This is child Thread: ");
        for (int i = 1; i <= 10; i++) 
        {
            System.out.println("Child Thread: "+i);    
            Thread.sleep(1000);
        }
      }
      catch(InterruptedException e)
      {
          System.out.println(e);  
      }
    }
}

 

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *