gpt4 book ai didi

复制 ThreadGroup 数组

转载 作者:知者 更新时间:2024-03-13 03:55:47 26 4
gpt4 key购买 nike

一 点睛

ThreadGroup 复制线程组的两个方法。
public int enumerate(ThreadGroup list[]) // 相对于 enumerate(list,true)

public int enumerate(ThreadGroup list[], boolean recurse) // recuese 控制是否递归复制线程组

二 实战

1 代码

package concurrent;

import java.util.concurrent.TimeUnit;

public class EnumerateThreadGroup {
    public static void main(String[] args) {
        ThreadGroup myGroup1 = new ThreadGroup("MyGroup1");
        ThreadGroup myGroup2 = new ThreadGroup(myGroup1, "MyGroup2");

        try {
            TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup[] list = new ThreadGroup[mainGroup.activeCount()];

        int recurseSize = mainGroup.enumerate(list);
        System.out.println(recurseSize);

        recurseSize = mainGroup.enumerate(list, false);
        System.out.println(recurseSize);
    }
}

2 测试

2

1

3 说明

myGroup1 的父 group 为 mainGroup,而 myGroup2 的父 group 为 myGroup1,因此上述代码运行之后,递归复制的结果为 2,不递归的情况为 1。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com