六、JAVA多线程:ThreadGroup

      在Java程序中,默认情况下,新的线程都会加入到main线程所在的group中,main线程的group名称同线程名。

如同线程存在父子关系一样,ThreadGroup也存在父子关系。

线程都会被加入到某个Thread Group中。

 

创建ThreadGroup

ThreadGroup(String name)

ThreadGroup(ThreadGroup parent, String name)

代码样例:

public class TheadGroupCreator {

    public static void main(String[] args) {

        // 获取当前程序的group
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();

        // 定义一个新的group
        ThreadGroup group1 = new ThreadGroup("group1") ;


        // 程序输出 ture
        System.out.println(group1.getParent() == currentGroup);

        // 定义group2 指定group1为其父group
        ThreadGroup group2 = new ThreadGroup(group1,"group2") ;


        // 程序输出 true
        System.out.println(group2.getParent() == group1);
        

    }
    
}

复制Thead数组

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

上面两个方法,会将TheadGroup中所有的active线程全部复制到Thread数组中,其中recure参数如果为true。

则方法会将所有子group中的acitve线程都递归到Thread数组中,共同调用方法:

ThreadGroup数组

public int enumerate(ThreadGroup[] list)
public int enumerate(ThreadGroup[] list, boolean recurse)  //recurse 是否以递归的方式复制
private int enumerate(ThreadGroup list[], int n, boolean recurse) {
    int ngroupsSnapshot = 0;
    ThreadGroup[] groupsSnapshot = null;
    synchronized (this) {
        if (destroyed) {
            return 0;
        }
        int ng = ngroups;
        if (ng > list.length - n) {
            ng = list.length - n;
        }
        if (ng > 0) {
            System.arraycopy(groups, 0, list, n, ng);
            n += ng;
        }
        if (recurse) {
            ngroupsSnapshot = ngroups;
            if (groups != null) {
                groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
            } else {
                groupsSnapshot = null;
            }
        }
    }
    if (recurse) {
        for (int i = 0 ; i < ngroupsSnapshot ; i++) {
            n = groupsSnapshot[i].enumerate(list, n, true);
        }
    }
    return n;
}

ThreadGroup操作

ThreadGroup并不能提供对线程的管理,主要功能是对线程进行组织。

官方API:

https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadGroup.html

ThreadGroup的interrupt

interrupt 一个ThreadGroup会导致该group 中所有的acitve线程都被interrupt。

public final void interrupt() {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        checkAccess();
        for (int i = 0 ; i < nthreads ; i++) {
            threads[i].interrupt();
        }
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
    }
    for (int i = 0 ; i < ngroupsSnapshot ; i++) {
        groupsSnapshot[i].interrupt();
    }
}

ThreadGroup的destroy

destroy用于销毁ThreadGroup ,针对没有任何active线程的group 进行destroy标记。

public final void destroy()

Destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

ThreadGroup的daemon

线程组可以设置为守护ThreadGroup,若将一个ThreadGroup设置为daemon,并不会影响线程的daemon。

如果一个ThreadGroup的daemon设置为true,那么该group中没有任何active线程的时候,该gruop会自动destory。

本文整理来源于:

《Java高并发编程详解:多线程与架构设计》 --汪文君

猜你喜欢

转载自blog.csdn.net/zhanglong_4444/article/details/86070018