threadgroup_Java ThreadGroup类activeGroupCount()方法与示例

threadgroup

ThreadGroup类activeGroupCount()方法 (ThreadGroup class activeGroupCount() method)

  • activeGroupCount() method is available in java.lang package.

    activeGroupCount()方法在java.lang包中可用。

  • activeGroupCount() method is used to return all active thread groups in this thread group and its subgroup also. The returned result is estimated so that it returns the number of active thread group in this thread group and it may change at runtime.

    activeGroupCount()方法用于返回此线程组及其子组中的所有活动线程组。 估计返回的结果,以便返回该线程组中活动线程组的数量,并且在运行时可能会更改。

  • activeGroupCount() method is a static method so it is accessible with the class name too.

    activeGroupCount()方法是静态方法,因此也可以使用类名进行访问。

  • activeGroupCount() method is not a final method, so it is overridable (i.e. this method is overridable in child class if we want).

    activeGroupCount()方法不是最终方法,因此它是可重写的(即,如果需要,此方法在子类中是可重写的)。

Syntax:

句法:

    public static int activeGroupCount();

Parameter(s):

参数:

  • This method does not accept any parameter.

    此方法不接受任何参数。

Return value:

返回值:

The return type of this method is int, it returns all the active thread groups in this thread group.

该方法的返回类型为int ,它返回该线程组中的所有活动线程组。

Example:

例:

// Java program to demonstrate the example of 
// activeGroupCount() method of ThreadGroup Class.

import java.lang.*;

class ActiveGroupCount extends Thread {
    // Override run() of Thread class
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + " " + "finish executing");
    }
}

public class Main {
    public static void main(String[] args) {
        ActiveGroupCount gac = new ActiveGroupCount();

        try {

            // 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, gac, "First Thread");
            Thread th2 = new Thread(tg2, gac, "Second Thread");

            // Calling start() method with Thread class object 
            // of Thread class

            th1.start();
            th2.start();

            // Here we are counting active thread in ThreadGroup

            System.out.print("Active Group in : " + tg1.getName() + "-");
            System.out.println(tg1.activeGroupCount());

            System.out.print("Active Group in : " + tg2.getName() + "-");
            System.out.println(tg2.activeGroupCount());


            th1.join();
            th2.join();

        } catch (Exception ex) {

            System.out.println(ex.getMessage());
        }
    }
}

Output

输出量

E:\Programs>javac Main.java
E:\Programs>java Main
First Thread finish executing
Second Thread finish executing
Active Group in : ThreadGroup 1-0
Active Group in : ThreadGroup 2-0


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

threadgroup

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值