Java - ThreadGroup setDaemon() Method with Examples



Description

The Java ThreadGroup setDaemon() method changes the daemon status of this thread group. A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

Declaration

Following is the declaration for java.lang.ThreadGroup.setDaemon() method

public final void setDaemon(boolean daemon)

Parameters

daemon − if true, marks this thread group as a daemon thread group; otherwise, marks this thread group as normal.

Return Value

This method does not return any value.

Exception

SecurityException − if the current thread cannot modify this thread group.

Example 1

The following example shows the usage of ThreadGroup setDaemon() method in case of a single ThreadGroup object. We've created a ThreadGroup object and assigned it a name. Then we've created two threads using the threadgroup object created earlier. Using setDaemon() method, we're setting it as a Daemon thread group. Using isDaemon() method, we're getting the status of the parent of this thread group object.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");
         // daemon status is set to true
         threadGroup.setDaemon(true);
         
         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // returns true if this thread group is a daemon thread group
         System.out.println("Is " + threadGroup.getName() + " a daemon ThreadGroup? " 
            + threadGroup.isDaemon());

         // block until the other threads finish
         t1.join();
         t2.join();        


      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread-0...
Starting Thread-1...
Is ThreadGroup a daemon ThreadGroup? true
Thread-1 finished executing.
Thread-0 finished executing.

Example 2

The following example shows the usage of ThreadGroup isDaemon() method in case of multiple ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Using setDaemon() method, we're setting it as a Daemon thread group. Then we've created two threads using the threadgroup objects created earlier. Using isDaemon() method, we're printing status each threadgroup object.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");
         // daemon status is set to true
         cThreadGroup.setDaemon(true);
         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // returns the status of thread group
         System.out.println("Is " + pThreadGroup.getName() 
            + " a daemon ThreadGroup? " + pThreadGroup.isDaemon());
         System.out.println("Is " + cThreadGroup.getName() 
            + " a daemon ThreadGroup? " + cThreadGroup.isDaemon());
         
         // block until the other threads finish
         t1.join();
         t2.join();

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread-0...
Starting Thread-1...
Is parent ThreadGroup a daemon ThreadGroup? false
Is child ThreadGroup a daemon ThreadGroup? true
Thread-1 finished executing.
Thread-0 finished executing.

Example 3

The following example shows the usage of ThreadGroup isDaemon() method in case of child and grandchild ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Using setDaemon() method, we're setting parent thread group as a Daemon thread group. Then we've created two threads using the child and grandchild threadgroup objects created earlier. Using isDaemon() method, we're printing status of each threadgroup object. As parent threadgroup is set as daemon, its child and ancestor become daemon as well.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 // daemon status is set to true
         pThreadGroup.setDaemon(true);
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a grandchild ThreadGroup for parent ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // returns the status of thread group
         System.out.println("Is " + pThreadGroup.getName() 
            + " a daemon ThreadGroup? " + pThreadGroup.isDaemon());
         System.out.println("Is " + cThreadGroup.getName() 
            + " a daemon ThreadGroup? " + cThreadGroup.isDaemon());
         System.out.println("Is " + gThreadGroup.getName() 
            + " a daemon ThreadGroup? " + gThreadGroup.isDaemon());		 
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread-0...
Starting Thread-1...
Is Parent ThreadGroup a daemon ThreadGroup? true
Is Child ThreadGroup a daemon ThreadGroup? true
Is GrandChild ThreadGroup a daemon ThreadGroup? true
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
Advertisements