Java - ThreadGroup getName() 方法



描述

Java ThreadGroup getName() 方法返回该线程组的名称。

声明

以下是 java.lang.ThreadGroup.getName() 方法的声明

public final String getName()

参数

返回值

此方法返回该线程组的名称。

异常

获取 ThreadGroup 对象名称示例

以下示例展示了在单个 ThreadGroup 对象的情况下 ThreadGroup getName() 方法的使用。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。使用 getName() 方法,我们获取了 thread group 对象的名称。

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 thread group
         String name = threadGroup.getName();
         System.out.println("Name of 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 threadGroup = ThreadGroup
Thread-1 finished executing.
Thread-0 finished executing.

在多个 ThreadGroup 对象中获取 ThreadGroup 名称的示例

以下示例展示了在多个 ThreadGroup 对象的情况下 ThreadGroup getName() 方法的使用。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。使用 getName() 方法,我们打印每个 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 name of thread group
         String name = pThreadGroup.getName();
         System.out.println("Name of pThreadGroup = " + name);

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

在子/孙 ThreadGroup 对象中获取 ThreadGroup 父对象的示例

以下示例展示了在子 ThreadGroup 对象和孙子 ThreadGroup 对象的情况下 ThreadGroup getName() 方法的使用。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的子 ThreadGroup 对象和孙子 ThreadGroup 对象创建了两个线程。使用 getName() 方法,我们打印每个 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 name of thread group
         String name = pThreadGroup.getName();
         System.out.println("Name of pThreadGroup = " + name);

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

         name = gThreadGroup.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 = Parent ThreadGroup
Name of cThreadGroup = Child ThreadGroup
Name of gThreadGroup = GrandChild ThreadGroup
Thread-1 finished executing.
Thread-0 finished executing.
java_lang_threadgroup.htm
广告