threadgroup_Java Thread类的最终ThreadGroup getThreadGroup()方法(带示例)

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


翻译自: https://www.includehelp.com/java/thread-class-final-threadgroup-getthreadgroup-method-with-example.aspx

threadgroup

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值