ThreadGroup类getParent()方法
- getParent() 方法在 java.lang 包中可用。
- getParent() 方法用于返回此线程组的父级。
- getParent() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
- getParent() 方法可能会在返回此 ThreadGroup 的父级时抛出异常。
SecurityException– 当不允许当前线程更新此线程组时,可能会抛出此异常。
用法:
public final ThreadGroup getParent();
参数:
- 它不接受任何参数。
返回值:
这个方法的返回类型是ThreadGroup
– 它返回这个 "ThreadGroup" 的父级,或者当没有这个线程组的父级时它返回 null。
例:
// Java program to demonstrate the example
// of ThreadGroup getParent() method of ThreadGroup
public class GetParent implements Runnable {
public static void main(String[] args) {
GetParent gp = new GetParent();
gp.parent();
}
public void parent() {
try {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
// By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
// By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
System.out.println();
// By using getParent() method is to return the
// parent of the base and derived thread group
System.out.println("base.getParent():" + " " + base.getParent().getName());
System.out.println("derived.getParent():" + " " + derived.getParent().getName());
// By using join() method is to wait the current
// thread till complete execution of another
// thread
th1.join();
th2.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
for (int k = 0; k < 100; ++k)
++k;
System.out.println(Thread.currentThread().getName() + " " + "ends.....");
}
}
输出
Thread-0 begins..... Thread-0 ends..... Thread-1 begins..... Thread-1 ends..... base.getParent():main derived.getParent():Base ThreadGroup
相关用法
- Java ThreadGroup getMaxPriority()用法及代码示例
- Java ThreadGroup getName()用法及代码示例
- Java ThreadGroup enumerate()用法及代码示例
- Java ThreadGroup parentOf()用法及代码示例
- Java ThreadGroup interrupt()用法及代码示例
- Java ThreadGroup suspend()用法及代码示例
- Java ThreadGroup setDaemon()用法及代码示例
- Java ThreadGroup isDaemon()用法及代码示例
- Java ThreadGroup checkAccess()用法及代码示例
- Java ThreadGroup resume()用法及代码示例
- Java ThreadGroup isDestroyed()用法及代码示例
- Java ThreadGroup toString()用法及代码示例
- Java ThreadGroup destroy()用法及代码示例
- Java ThreadGroup list()用法及代码示例
- Java ThreadGroup stop()用法及代码示例
- Java ThreadGroup activeCount()用法及代码示例
- Java ThreadGroup setMaxPriority()用法及代码示例
- Java ThreadGroup activeGroupCount()用法及代码示例
- Java Thread toString()用法及代码示例
- Java Thread interrupted()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java ThreadGroup getParent() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。