Java - ThreadGroup getParent() 方法



描述

Java ThreadGroup getParent() 方法返回該執行緒組的父執行緒組的名稱。

宣告

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

public final ThreadGroup getParent()

引數

返回值

此方法返回此執行緒組的父級。頂級執行緒組是唯一父級為 null 的執行緒組。

異常

獲取 ThreadGroup 父級的示例

以下示例展示了在單個 ThreadGroup 物件的情況下 ThreadGroup getParent() 方法的使用。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 getParent() 方法,我們獲取了此執行緒組物件的父級的名稱,該名稱將為 main。

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 name of parent of thread group
         String name = threadGroup.getParent().getName();
         System.out.println("Name of parent of the threadGroup = " + name);

         // 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...
Name of parent of the threadGroup = main
Thread-1 finished executing.
Thread-0 finished executing.

在多個 ThreadGroup 物件中獲取 ThreadGroup 父級的示例

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

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 name of thread group
         String name = pThreadGroup.getParent().getName();
         System.out.println("Name of parent of pThreadGroup = " + name);

         name = cThreadGroup.getParent().getName();
         System.out.println("Name of parent of cThreadGroup = " + name);
         // 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...
Name of parent of pThreadGroup = main
Name of parent of cThreadGroup = parent ThreadGroup
Thread-0 finished executing.
Thread-1 finished executing.

在子/孫 ThreadGroup 物件中獲取 ThreadGroup 父級的示例

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

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 name of the parent of thread group
         String name = pThreadGroup.getParent().getName();
         System.out.println("Name of pThreadGroup = " + name);

         name = cThreadGroup.getParent().getName();
         System.out.println("Name of cThreadGroup = " + name);

         name = gThreadGroup.getParent().getName();
         System.out.println("Name of gThreadGroup = " + name);

         // 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...
Name of pThreadGroup = main
Name of cThreadGroup = Parent ThreadGroup
Name of gThreadGroup = Child ThreadGroup
Thread-1 finished executing.
Thread-0 finished executing.
java_lang_threadgroup.htm
廣告