Java - ThreadGroup isDestroyed() 方法



描述

Java ThreadGroup isDestroyed() 方法測試此執行緒組是否已被銷燬。

宣告

以下是java.lang.ThreadGroup.isDestroyed() 方法的宣告

public boolean isDestroyed()

引數

返回值

如果此物件已被銷燬,則此方法返回 true。

異常

檢查 ThreadGroup 是否已銷燬的示例

以下示例展示了在單個 ThreadGroup 物件的情況下使用 ThreadGroup isDestroyed() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。然後,我們使用前面建立的 threadgroup 物件建立了兩個執行緒。使用 isDestroyed() 方法,我們獲取此 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 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 isDestroyed = threadGroup.isDestroyed();
         System.out.println("Status of the threadGroup = " + isDestroyed);

         // 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 物件的情況下使用 ThreadGroup isDestroyed() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的 threadgroup 物件建立了兩個執行緒。使用 isDestroyed() 方法,我們列印每個 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");

         // 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.isDestroyed());
         System.out.println("Status of cThreadGroup = " + cThreadGroup.isDestroyed());
         
         // 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 isDestroyed() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。使用 isDestroyed() 方法,我們列印每個 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");
		 
         // 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.isDestroyed());
         System.out.println("Status of cThreadGroup = " + cThreadGroup.isDestroyed());
         System.out.println("Status of gThreadGroup = " + gThreadGroup.isDestroyed());
		 
         // 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
廣告