Java ThreadGroup 类

描述

Java ThreadGroup toString()方法返回此线程组的字符串表示形式。

声明

以下内容是java.lang.ThreadGroup.toString()方法的声明

public String toString()

参数

返回值

此方法返回返回此线程组的字符串表示形式。

异常

示例 1

以下内容该示例显示了在单个 ThreadGroup 对象的情况下 ThreadGroup toString() 方法的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。然后我们使用之前创建的线程组对象创建了两个线程。使用 toString() 方法,我们获取线程组对象的字符串表示形式。

package com.yxjc123;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         //创建一个ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();

         //返回线程组的字符串表示
         System.out.println("String representation of threadGroup = " + threadGroup.toString());

         // 阻塞直到其他线程完成
         t1.join();
         t2.join();        


      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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.

示例 2

以下示例显示了在多个 ThreadGroup 对象的情况下 ThreadGroup toString() 方法的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后我们使用之前创建的线程组对象创建了两个线程。使用 toString() 方法,我们打印每个线程组对象的字符串表示形式。

package com.yxjc123;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         //创建父ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();

         //返回线程组的字符串表示形式
         System.out.println("String representation of pThreadGroup = " + pThreadGroup.toString());
         System.out.println("String representation of cThreadGroup = " + cThreadGroup.toString());
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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.

示例 3

以下示例显示了 ThreadGroup toString() 方法在子 ThreadGroup 和孙 ThreadGroup 对象的情况下的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用之前创建的子线程组对象和孙线程组对象创建了两个线程。使用 toString() 方法,我们打印每个线程组对象的字符串表示形式。

package com.yxjc123;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         //创建父ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         //为父ThreadGroup创建孙ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         //返回线程组的字符串表示形式
         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());
		 
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			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.