浅析java的enumerate方法

enumerate方法用来将ThreadGroup线程组中的active线程全部复制到Thread类型的数组中,并且返回数组中元素个数,即线程组中active线程数量。有以下两种调用方法:

   public int enumerate(Thread[] list) {
        this.checkAccess();
        return this.enumerate((Thread[])list, 0, true);
    }
 public int enumerate(Thread[] list, boolean recurse) {
        this.checkAccess();
        return this.enumerate((Thread[])list, 0, recurse);
    }

上述两种方法都调用了ThreadGroup的私有方法enumerate:

private int enumerate(Thread[] list, int n, boolean recurse) {
        int ngroupsSnapshot = 0;
        ThreadGroup[] groupsSnapshot = null;
        synchronized(this) {
            if (this.destroyed) {
                return 0;
            }

            int nt = this.nthreads;
            if (nt > list.length - n) {
                nt = list.length - n;
            }

            for(int i = 0; i < nt; ++i) {
                if (this.threads[i].isAlive()) {
                    list[n++] = this.threads[i];
                }
            }

            if (recurse) {
                ngroupsSnapshot = this.ngroups;
                if (this.groups != null) {
                    groupsSnapshot = (ThreadGroup[])Arrays.copyOf(this.groups, ngroupsSnapshot);
                } else {
                    groupsSnapshot = null;
                }
            }
        }

        if (recurse) {
            for(int i = 0; i < ngroupsSnapshot; ++i) {
                n = groupsSnapshot[i].enumerate(list, n, true);
            }
        }

        return n;
    }

从上述代码可以看出,enumerate(Thread[] list)方法实际上就是enumerate(Thread[] list,true)的方法。那么形参recurse设置为true或者false到底有什么区别呢?事实上,true的情况是将所有子group中的active线程都递归到Thread数组中,而false的情况则是将ThreadGroup(父线程组)中的active线程全部复制到Thread数组中。我们通常称true的情况是递归方法,而false的方法是非递归方法。因为看一下私有方法enumerate的代码实现会发现二者就差了递归调用的方法。下面是具体的代码实现:

import java.util.concurrent.TimeUnit;

public class ThreadGroupEnumerateThreads {
    public static void main(String[] args) throws InterruptedException {
        ThreadGroup myGroup=new ThreadGroup("MyGroup");
        Thread thread=new Thread(myGroup,()->
        {
            while(true){
                try{
                    TimeUnit.SECONDS.sleep(1);
                }catch(InterruptedException e){

                    e.printStackTrace();
                }
            }
        },"MyThread");
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2);
        ThreadGroup mainGroup=Thread.currentThread().getThreadGroup();
        Thread[] list=new Thread[mainGroup.activeCount()];
        int recurseSize=mainGroup.enumerate(list);
        System.out.println(recurseSize);
        recurseSize=mainGroup.enumerate(list,false);
        System.out.println(recurseSize);
    }
}

运行结果分别输出3和2。
关系图如下所示:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值