Java ThreadGroup 类

说明

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

声明

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

public final void setDaemon(boolean daemon) 

参数

daemon 如果为 true,则将此线程组标记为守护线程组;否则,将此线程组标记为正常。

返回值

此方法不返回任何值。

异常

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

示例 1

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

package com.yxjc123;

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

   public void start() {
      try {     
         //创建一个ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");
         //守护进程状态设置为true
         threadGroup.setDaemon(true);
         
         //创建一个线程
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         //如果该线程组是守护线程组则返回true
         System.out.println("Is " + threadGroup.getName() + " a daemon ThreadGroup? " 
            + threadGroup.isDaemon());

         // 阻塞直到其他线程完成
         t1.join();
         t2.join();        


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

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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. 

示例 2

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

package com.yxjc123;

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

   public void start() {
      try {     
         //创建父ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");
         //守护进程状态设置为true
         cThreadGroup.setDaemon(true);
         //创建一个线程
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         //返回线程组的状态
         System.out.println("Is " + pThreadGroup.getName() 
            + " a daemon ThreadGroup? " + pThreadGroup.isDaemon());
         System.out.println("Is " + cThreadGroup.getName() 
            + " a daemon ThreadGroup? " + cThreadGroup.isDaemon());
         
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();

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

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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. 

示例 3

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

package com.yxjc123;

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

   public void start() {
      try {     
         //创建父ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 //守护进程状态设置为true
         pThreadGroup.setDaemon(true);
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         //为父ThreadGroup创建孙ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         //返回线程组的状态
         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());		 
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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.