Java - ThreadGroup setDaemon() 方法



描述

Java ThreadGroup setDaemon() 方法更改此线程组的守护进程状态。当其最后一个线程停止或其最后一个线程组被销毁时,守护进程线程组会自动销毁。

声明

以下是 java.lang.ThreadGroup.setDaemon() 方法的声明

public final void setDaemon(boolean daemon)

参数

daemon − 如果为 true,则将此线程组标记为守护进程线程组;否则,将此线程组标记为普通线程组。

返回值

此方法不返回值。

异常

SecurityException − 如果当前线程无法修改此线程组。

在 ThreadGroup 对象中设置守护进程线程示例

以下示例显示了在单个 ThreadGroup 对象的情况下使用 ThreadGroup setDaemon() 方法的情况。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。使用 setDaemon() 方法,我们将其设置为守护进程线程组。使用 isDaemon() 方法,我们获取此线程组对象的父级的状态。

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.");
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

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

在多个 ThreadGroup 对象中设置守护进程线程示例

以下示例显示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup isDaemon() 方法的情况。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。使用 setDaemon() 方法,我们将其设置为守护进程线程组。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。使用 isDaemon() 方法,我们打印每个 threadgroup 对象的状态。

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.");
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

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.

在子/孙 ThreadGroup 对象中设置守护进程线程示例

以下示例显示了在子线程组和孙线程组对象的情况下使用 ThreadGroup isDaemon() 方法的情况。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。使用 setDaemon() 方法,我们将父线程组设置为守护进程线程组。然后,我们使用前面创建的子线程组和孙线程组对象创建了两个线程。使用 isDaemon() 方法,我们打印每个 threadgroup 对象的状态。由于父线程组设置为守护进程,因此其子线程组和祖先线程组也成为守护进程。

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.");
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

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
广告