Java - ThreadGroup parentOf() 方法



描述

Java ThreadGroup parentOf() 方法測試此執行緒組是否為執行緒組引數或其祖先執行緒組之一。

宣告

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

public final boolean parentOf(ThreadGroup g)

引數

g − 一個執行緒組。

返回值

如果此執行緒組是執行緒組引數或其祖先執行緒組之一,則此方法返回 true;否則返回 false。

異常

檢查 ThreadGroup 物件的父級示例

以下示例演示了在 ThreadGroup 物件的情況下 ThreadGroup parentOf() 方法的用法。我們建立了一個父 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 parentOf() 方法,我們列印子執行緒組物件的父級檢查。

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

         // determine which ThreadGroup is parent
         boolean isParent = pThreadGroup.parentOf(cThreadGroup);
         System.out.println(pThreadGroup.getName() + " is the parent of "
            + cThreadGroup.getName() + "? " + isParent);
         
         // 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...
parent ThreadGroup is the parent of child ThreadGroup? true
Thread-1 finished executing.
Thread-0 finished executing.

檢查多個 ThreadGroup 物件的父級示例

以下示例演示了在多個 ThreadGroup 物件的情況下 ThreadGroup parentOf() 方法的用法。我們建立了一個父 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 parentOf() 方法,我們列印父執行緒組物件的父級檢查。

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

         // determine which ThreadGroup is parent
         boolean isParent = cThreadGroup.parentOf(pThreadGroup);
         System.out.println(cThreadGroup.getName() + " is the parent of "
            + pThreadGroup.getName() + "? " + isParent);
         
         // 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...
child ThreadGroup is the parent of parent ThreadGroup? false
Thread-1 finished executing.
Thread-0 finished executing.

檢查子/孫子 ThreadGroup 物件的父級示例

以下示例演示了在子執行緒組和孫子執行緒組的情況下 ThreadGroup parentOf() 方法的用法。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的子執行緒組和孫子執行緒組物件建立了兩個執行緒。使用 parentOf() 方法,我們列印每個執行緒組物件的父級的名稱。

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 child 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();

         // determine which ThreadGroup is parent
         boolean isParent = pThreadGroup.parentOf(cThreadGroup);
         System.out.println(pThreadGroup.getName() + " is the parent of "
            + cThreadGroup.getName() + "? " + isParent);
			
         isParent = pThreadGroup.parentOf(gThreadGroup);
         System.out.println(pThreadGroup.getName() + " is the parent of "
            + gThreadGroup.getName() + "? " + isParent);
         
		 // 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...
parent ThreadGroup is the parent of child ThreadGroup? true
parent ThreadGroup is the parent of grandchild ThreadGroup? true
Thread-1 finished executing.
Thread-0 finished executing.
java_lang_threadgroup.htm
廣告