- 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 parentOf() 方法
描述
Java ThreadGroup parentOf() 方法测试此线程组是否为线程组参数或其祖先线程组之一。
声明
以下是java.lang.ThreadGroup.parentOf() 方法的声明
public final boolean parentOf(ThreadGroup g)
参数
g − 一个线程组。
返回值
如果此线程组是线程组参数或其祖先线程组之一,则此方法返回 true;否则返回 false。
异常
无
检查 ThreadGroup 对象的父级示例
以下示例演示了在 ThreadGroup 对象的情况下 ThreadGroup parentOf() 方法的用法。我们创建了一个父 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的线程组对象创建了两个线程。使用 parentOf() 方法,我们打印子线程组对象的父级检查。
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(); // determine which ThreadGroup is parent boolean isParent = pThreadGroup.parentOf(cThreadGroup); System.out.println(pThreadGroup.getName() + " is the parent of " + cThreadGroup.getName() + "? " + isParent); // 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... parent ThreadGroup is the parent of child ThreadGroup? true Thread-1 finished executing. Thread-0 finished executing.
检查多个 ThreadGroup 对象的父级示例
以下示例演示了在多个 ThreadGroup 对象的情况下 ThreadGroup parentOf() 方法的用法。我们创建了一个父 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的线程组对象创建了两个线程。使用 parentOf() 方法,我们打印父线程组对象的父级检查。
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(); // determine which ThreadGroup is parent boolean isParent = cThreadGroup.parentOf(pThreadGroup); System.out.println(cThreadGroup.getName() + " is the parent of " + pThreadGroup.getName() + "? " + isParent); // 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... child ThreadGroup is the parent of parent ThreadGroup? false Thread-1 finished executing. Thread-0 finished executing.
检查子/孙子 ThreadGroup 对象的父级示例
以下示例演示了在子线程组和孙子线程组的情况下 ThreadGroup parentOf() 方法的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的子线程组和孙子线程组对象创建了两个线程。使用 parentOf() 方法,我们打印每个线程组对象的父级的名称。
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 child 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(); // determine which ThreadGroup is parent boolean isParent = pThreadGroup.parentOf(cThreadGroup); System.out.println(pThreadGroup.getName() + " is the parent of " + cThreadGroup.getName() + "? " + isParent); isParent = pThreadGroup.parentOf(gThreadGroup); System.out.println(pThreadGroup.getName() + " is the parent of " + gThreadGroup.getName() + "? " + isParent); // 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... parent ThreadGroup is the parent of child ThreadGroup? true parent ThreadGroup is the parent of grandchild ThreadGroup? true Thread-1 finished executing. Thread-0 finished executing.