Java - ThreadGroup isDaemon() 方法



描述

Java ThreadGroup isDaemon() 方法测试此线程组是否为守护线程组。当守护线程组的最后一个线程停止或其最后一个线程组被销毁时,它将自动被销毁。

声明

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

public final boolean isDaemon()

参数

返回值

如果此线程组是守护线程组,则此方法返回 true,否则返回 false。

异常

在 ThreadGroup 对象中检查守护线程示例

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

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

         // 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 the status of thread group
         boolean isDaemon = threadGroup.isDaemon();
         System.out.println("Status of the 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...
Status of the threadGroup = false
Thread-1 finished executing.
Thread-0 finished executing.

在多个 ThreadGroup 对象中检查守护线程示例

以下示例显示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup isDaemon() 方法的情况。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的线程组对象创建了两个线程。使用 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 parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");

         // 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("Status of pThreadGroup = " + pThreadGroup.isDaemon());
         System.out.println("Status of cThreadGroup = " + 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...
Status of pThreadGroup = false
Status of cThreadGroup = false
Thread-1 finished executing.
Thread-0 finished executing.

在子/孙 ThreadGroup 对象中检查守护线程示例

以下示例显示了在子线程组和孙线程组对象的情况下使用 ThreadGroup isDaemon() 方法的情况。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的子线程组和孙线程组对象创建了两个线程。使用 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 parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // 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("Status of pThreadGroup = " + pThreadGroup.isDaemon());
         System.out.println("Status of cThreadGroup = " + cThreadGroup.isDaemon());
         System.out.println("Status of gThreadGroup = " + 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...
Status of pThreadGroup = false
Status of cThreadGroup = false
Status of gThreadGroup = false
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
广告