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