1. 程式人生 > >六、JAVA多執行緒:ThreadGroup

六、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高併發程式設計詳解:多執行緒與架構設計》 --汪文君