threadgroup
线程类最终ThreadGroup getThreadGroup() (Thread Class final ThreadGroup getThreadGroup())
This method is available in package java.lang.Thread.getThreadGroup().
软件包java.lang.Thread.getThreadGroup()中提供了此方法。
This method is used to return the ThreadGroup of this thread [i.e. It represents that this thread basically belongs to which ThreadGroup].
此方法用于返回该线程的ThreadGroup [即,它表示该线程基本上属于哪个ThreadGroup]。
This method is final so we can't override this method in child class.
此方法是最终方法,因此我们不能在子类中覆盖此方法。
The return type of this method is ThreadGroup so it returns the Threadgroup of this thread that means our thread basically belongs to which group.
该方法的返回类型为ThreadGroup,因此它返回该线程的Threadgroup,这意味着我们的线程基本上属于哪个组。
This method does not raise any exception.
此方法不会引发任何异常。
Syntax:
句法:
final ThreadGroup getThreadGroup(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Thread.
我们不会在Thread方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is ThreadGroup, it returns the ThreadGroup of this thread.
该方法的返回类型为ThreadGroup ,它返回此线程的ThreadGroup。
Java程序演示getThreadGroup()方法的示例 (Java program to demonstrate example of getThreadGroup() method)
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class GetThreadGroup extends Thread {
// Override run() of Thread class
public void run() {
System.out.println("We are in run() method");
}
}
class Main {
public static void main(String[] args) {
// Creating an object of GetThreadGroup class
GetThreadGroup gt_group = new GetThreadGroup();
// We are creating an object of ThreadGroup class
ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");
// We are creating an object of Thread class and
// we are assigning the ThreadGroup of both the thread
Thread th1 = new Thread(tg1, gt_group, "First Thread");
Thread th2 = new Thread(tg2, gt_group, "Second Thread");
// Calling start() method with Thread class object of Thread class
th1.start();
th2.start();
// Here we are displaying which thread is basically
// belongs to which group
System.out.println("The " + th1.getName() + " " + "is belongs to" + th1.getThreadGroup().getName());
System.out.println("The " + th2.getName() + " " + "is belongs to" + th2.getThreadGroup().getName());
}
}
Output
输出量
E:\Programs>javac Main.java
E:\Programs>java Main
The First Thread is belongs toThreadGroup 1
We are in run() method
We are in run() method
The Second Thread is belongs toThreadGroup 2
threadgroup