This is the 18th day of my participation in the August Challenge

preface

ThreadGroup → For threads, users manage threads in batches, such as batch stops or suspends, using the concept of thread groups.

1. The Thread and ThreadGroup

In Java programs, by default new threads are added to the group of the main Thread, which has the same name as the Thread. Like parent threads, threadgroups have parent relationships. In any case, threads are added to a ThreadGroup.

2. Create ThreadGroup

  • public ThreadGroup (String name);
  • public ThreadGroup (ThreadGroup parent , String name);

The syntax for creating a ThreadGroup is very simple. The first constructor gives a name to a ThreadGroup whose parent class is the ThreadGroup of the thread that created the ThreadGroup. The second constructor displays the specified parent ThreadGroup and gives itself a name.

    public static void main(String[] args) {
        // Create a new thread group with a name. The parent thread group defaults to the current thread group.
        ThreadGroup threadGroup = new ThreadGroup("I'm a child thread group.");
        //true
        System.out.println(Thread.currentThread().getThreadGroup() == threadGroup.getParent());
        // Create a thread group, give the name + specify the parent thread group
        ThreadGroup threadGroup1 = new ThreadGroup(threadGroup,"Super child");
        //true
        System.out.println(threadGroup1.getParent() == threadGroup);
    }
Copy the code

3. Copy the Thread array and ThreadGroup array

Copy Thread array

  • public int enumerate (Thread[] list)
  • public int enumerate (Thread[] list , boolean recurse)

Recurse if true, recurse recurses all subgroups of active threads into the array. Enumerate (Thread[] list) equivalent to enumerate(Thread[] list, true)

Pay attention to

  • The number of threads retrieved by enumerate is only an estimate, and there is no guarantee that the number of active threads in the current group will be inaccurate if, for example, a thread ends its life cycle after replication is called or a new thread is added.
  • The return value int of enumerate is more realistic than the length of Thread[]. For example, if an array of threads has a specified length, enumerate will only separate the active threads into the array. The return value int represents the true number, not the length of the array

Copy the ThreadGroup array

  • public int enumerate(ThreadGroup[] list)
  • public int enumerate(ThreadGroup[] list, boolean recurse)

Similar to copying the Thread array, the recurse method is used to replicate the subgroups of the current ThreadGroup. Again, recurse determines whether to replicate recursively.

    public static void main(String[] args) throws InterruptedException {
        ThreadGroup myGroup1 = new ThreadGroup("MyGroup1");
        ThreadGroup myGroup2 = new ThreadGroup(myGroup1,"MyGroup2");
        TimeUnit.SECONDS.sleep(2);
        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);

    }
Copy the code

conclusion

Thread groups: You can manage threads or thread group objects in batches, effectively organizing them. All threads created by the user belong to the specified thread group, and if the specified thread group is not displayed, the thread belongs to the default thread group (the main thread group). By default, the child thread and parent thread are in the same thread group. The thread group can only be specified when a thread is created, and the thread group cannot be changed midway through its execution. That is to say, once a thread is specified in a thread group, it will remain in that thread until it terminates.