- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包其他内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - ThreadGroup 的 list() 方法
描述
Java ThreadGroup 的 list() 方法将有关此线程组的信息打印到标准输出。这仅对调试有用。
声明
以下是 java.lang.ThreadGroup.list() 方法的声明
public void list()
参数
无
返回值
此方法不返回值。
异常
无
打印单个 ThreadGroup 对象的调试信息示例
以下示例显示了在单个 ThreadGroup 对象的情况下使用 ThreadGroup list() 方法的情况。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。然后,我们使用前面创建的线程组对象创建了两个线程。使用 list() 方法,我们获取了此线程组对象的的信息。
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(); // print the details of the thread group threadGroup.list(); // 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... java.lang.ThreadGroup[name=ThreadGroup,maxpri=10] Thread[Thread-0,5,ThreadGroup] Thread[Thread-1,5,ThreadGroup] Thread-1 finished executing. Thread-0 finished executing.
打印多个 ThreadGroup 对象的调试信息示例
以下示例显示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup list() 方法的情况。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的线程组对象创建了两个线程。使用 list() 方法,我们打印每个线程组对象的信息。
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(); // print the details of the thread groups pThreadGroup.list(); cThreadGroup.list(); // 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... java.lang.ThreadGroup[name=parent ThreadGroup,maxpri=10] Thread[Thread-0,5,parent ThreadGroup] java.lang.ThreadGroup[name=child ThreadGroup,maxpri=10] Thread[Thread-1,5,child ThreadGroup] java.lang.ThreadGroup[name=child ThreadGroup,maxpri=10] Thread[Thread-1,5,child ThreadGroup] Thread-0 finished executing. Thread-1 finished executing.
打印子/孙 ThreadGroup 对象的调试信息示例
以下示例显示了在子线程组和孙线程组对象的情况下使用 ThreadGroup list() 方法的情况。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的子线程组和孙线程组对象创建了两个线程。使用 list() 方法,我们打印每个线程组对象的父对象名称。
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(); // print the details of the thread groups pThreadGroup.list(); cThreadGroup.list(); gThreadGroup.list(); // 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... java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10] java.lang.ThreadGroup[name=Child ThreadGroup,maxpri=10] Thread[Thread-0,5,Child ThreadGroup] java.lang.ThreadGroup[name=GrandChild ThreadGroup,maxpri=10] Thread[Thread-1,5,GrandChild ThreadGroup] java.lang.ThreadGroup[name=Child ThreadGroup,maxpri=10] Thread[Thread-0,5,Child ThreadGroup] java.lang.ThreadGroup[name=GrandChild ThreadGroup,maxpri=10] Thread[Thread-1,5,GrandChild ThreadGroup] java.lang.ThreadGroup[name=GrandChild ThreadGroup,maxpri=10] Thread[Thread-1,5,GrandChild ThreadGroup] Thread-1 finished executing. Thread-0 finished executing.
java_lang_threadgroup.htm
广告