Java - ThreadGroup toString() 方法



描述

Java ThreadGroup toString() 方法返回此執行緒組的字串表示形式。

宣告

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

public String toString()

引數

返回值

此方法返回此執行緒組的字串表示形式。

異常

獲取 ThreadGroup 的字串表示形式示例

以下示例演示了在單個 ThreadGroup 物件的情況下使用 ThreadGroup toString() 方法。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 toString() 方法,我們獲取了執行緒組物件的字串表示形式。

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.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();

         // returns the string representation of thread group
         System.out.println("String representation of threadGroup = " + threadGroup.toString());

         // 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().toString() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,ThreadGroup]...
Starting Thread[Thread-1,5,ThreadGroup]...
String representation of threadGroup = java.lang.ThreadGroup[name=ThreadGroup,maxpri=10]
Thread[Thread-1,5,ThreadGroup] finished executing.
Thread[Thread-0,5,ThreadGroup] finished executing.

獲取多個 ThreadGroup 物件的字串表示形式示例

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

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.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();

         // returns the string representation of thread groups
         System.out.println("String representation of pThreadGroup = " + pThreadGroup.toString());
         System.out.println("String representation of cThreadGroup = " + cThreadGroup.toString());
         // 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().toString() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,parent ThreadGroup]...
Starting Thread[Thread-1,5,child ThreadGroup]...
String representation of pThreadGroup = java.lang.ThreadGroup[name=parent ThreadGroup,maxpri=10]
String representation of cThreadGroup = java.lang.ThreadGroup[name=child ThreadGroup,maxpri=10]
Thread[Thread-1,5,child ThreadGroup] finished executing.
Thread[Thread-0,5,parent ThreadGroup] finished executing.

獲取子/孫 ThreadGroup 物件的字串表示形式示例

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

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.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         // returns the string representation of thread groups
         System.out.println("String representation of pThreadGroup = " + pThreadGroup.toString());
         System.out.println("String representation of cThreadGroup = " + cThreadGroup.toString());
         System.out.println("String representation of gThreadGroup = " + gThreadGroup.toString());
		 
         // 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().toString() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,Child ThreadGroup]...
Starting Thread[Thread-1,5,GrandChild ThreadGroup]...
String representation of pThreadGroup = java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
String representation of cThreadGroup = java.lang.ThreadGroup[name=Child ThreadGroup,maxpri=10]
String representation of gThreadGroup = java.lang.ThreadGroup[name=GrandChild ThreadGroup,maxpri=10]
Thread[Thread-0,5,Child ThreadGroup] finished executing.
Thread[Thread-1,5,GrandChild ThreadGroup] finished executing.
java_lang_threadgroup.htm
廣告