ThreadGroup


一、源碼

1、屬性

private final ThreadGroup parent;
父線程組對象
String name;
線程組名稱
int maxPriority;
最高優先級
boolean destroyed;
是否已銷毀
boolean daemon;
是否是守護線程
boolean vmAllowSuspension;
虛擬機自動掛起
int nUnstartedThreads = 0;
未啟動線程數
int nthreads;
線程總數
Thread threads[];
線程數組
int ngroups;
線程組數量
ThreadGroup groups[];
線程組數組

  

2、構造方法

private ThreadGroup() {     // called from C code
    this.name = "system";
    this.maxPriority = Thread.MAX_PRIORITY;
    this.parent = null;
}
public ThreadGroup(String name) {
    this(Thread.currentThread().getThreadGroup(), name);
}
public ThreadGroup(ThreadGroup parent, String name) {
    this(checkParentAccess(parent), parent, name);
}

private ThreadGroup(Void unused, ThreadGroup parent, String name) {
    this.name = name;
    this.maxPriority = parent.maxPriority;
    this.daemon = parent.daemon;
    this.vmAllowSuspension = parent.vmAllowSuspension;
    this.parent = parent;
    parent.add(this);
}

  

3、一般方法

private static Void checkParentAccess(ThreadGroup parent) {
    parent.checkAccess();
    return null;
}
確認權限
public final String getName() {
    return name;
}
獲得線程組名稱
public final ThreadGroup getParent() {
    if (parent != null)
        parent.checkAccess();
    return parent;
}
獲得父線程組
public final int getMaxPriority() {
    return maxPriority;
}
獲得最高優先級
public final boolean isDaemon() {
    return daemon;
}
是否守護線程
public synchronized boolean isDestroyed() {
    return destroyed;
}
是否已銷毀
public final void setDaemon(boolean daemon) {
    checkAccess();
    this.daemon = daemon;
}
設置守護線程
public final void checkAccess() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkAccess(this);
    }
}
確認權限

  

 

public final void setMaxPriority(int pri) {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        checkAccess();
        if (pri < Thread.MIN_PRIORITY || pri > Thread.MAX_PRIORITY) {
            return;
        }
        //如果有父分組,取優先級小的值
        maxPriority = (parent != null) ? Math.min(pri, parent.maxPriority) : pri;
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
    }
    //設置當前線程組中每組的優先級
    for (int i = 0 ; i < ngroupsSnapshot ; i++) {
        groupsSnapshot[i].setMaxPriority(pri);
    }
}

設置最大優先級

public final boolean parentOf(ThreadGroup g) {
    for (; g != null ; g = g.parent) {
        if (g == this) {
            return true;
        }
    }
    return false;
}

是否是其祖先線程組

public final void checkAccess() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkAccess(this);
    }
}

權限相關

public int activeCount() {
    int result;
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        if (destroyed) {
            return 0;
        }
        result = nthreads;
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
    }
    for (int i = 0 ; i < ngroupsSnapshot ; i++) {
        result += groupsSnapshot[i].activeCount();
    }
    return result;
}

獲得當前線程組和子孫線程組的線程總數

 

public int enumerate(Thread list[]) {
    checkAccess();
    return enumerate(list, 0, true);
}
public int enumerate(Thread list[], boolean recurse) {
    checkAccess();
    return enumerate(list, 0, recurse);
}
private int enumerate(Thread list[], int n, boolean recurse) {
    int ngroupsSnapshot = 0;
    ThreadGroup[] groupsSnapshot = null;
    synchronized (this) {
        if (destroyed) {
            return 0;
        }
        int nt = nthreads;
        if (nt > list.length - n) {
            nt = list.length - n;
        }
        for (int i = 0; i < nt; i++) {
            if (threads[i].isAlive()) {
                list[n++] = threads[i];
            }
        }
        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;
}

將線程組和子線程組的線程復制到數組中

public int activeGroupCount() {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        if (destroyed) {
            return 0;
        }
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
    }
    int n = ngroupsSnapshot;
    for (int i = 0 ; i < ngroupsSnapshot ; i++) {
        n += groupsSnapshot[i].activeGroupCount();
    }
    return n;
}

獲得線程組和子線程組的數量

public int enumerate(ThreadGroup list[]) {
    checkAccess();
    return enumerate(list, 0, true);
}
public int enumerate(ThreadGroup list[], boolean recurse) {
    checkAccess();
    return enumerate(list, 0, 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;
}

將線程組和子線程組復制到數組中

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();
    }
}

暫停所有線程

public final void destroy() {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        checkAccess();
        if (destroyed || (nthreads > 0)) {
            throw new IllegalThreadStateException();
        }
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
        if (parent != null) {
            destroyed = true;
            ngroups = 0;
            groups = null;
            nthreads = 0;
            threads = null;
        }
    }
    for (int i = 0 ; i < ngroupsSnapshot ; i += 1) {
        groupsSnapshot[i].destroy();
    }
    if (parent != null) {
        parent.remove(this);
    }
}

銷毀所有線程組和子組,包含的線程必須為空

private final void add(ThreadGroup g){
    synchronized (this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        if (groups == null) {
            groups = new ThreadGroup[4];
        } else if (ngroups == groups.length) {
            groups = Arrays.copyOf(groups, ngroups * 2);
        }
        groups[ngroups] = g;

        ngroups++;
    }
}

新增線程組,數組初始化是4個,之后每次滿了之后翻倍

private void remove(ThreadGroup g) {
    synchronized (this) {
        if (destroyed) {
            return;
        }
        for (int i = 0 ; i < ngroups ; i++) {
            if (groups[i] == g) {
                ngroups -= 1;
                System.arraycopy(groups, i + 1, groups, i, ngroups - i);
                groups[ngroups] = null;
                break;
            }
        }
        if (nthreads == 0) {
            notifyAll();
        }
        if (daemon && (nthreads == 0) &&
            (nUnstartedThreads == 0) && (ngroups == 0))
        {
            destroy();
        }
    }
}

移除線程組

void addUnstarted() {
    synchronized(this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        nUnstartedThreads++;
    }
}

增加線程組數量

void add(Thread t) {
    synchronized (this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        if (threads == null) {
            threads = new Thread[4];
        } else if (nthreads == threads.length) {
            threads = Arrays.copyOf(threads, nthreads * 2);
        }
        threads[nthreads] = t;

        nthreads++;

        nUnstartedThreads--;
    }
}

增加線程

void threadStartFailed(Thread t) {
    synchronized(this) {
        remove(t);
        nUnstartedThreads++;
    }
}

線程啟動失敗

void threadTerminated(Thread t) {
    synchronized (this) {
        remove(t);

        if (nthreads == 0) {
            notifyAll();
        }
        if (daemon && (nthreads == 0) &&
            (nUnstartedThreads == 0) && (ngroups == 0))
        {
            destroy();
        }
    }
}

終止線程

private void remove(Thread t) {
    synchronized (this) {
        if (destroyed) {
            return;
        }
        for (int i = 0 ; i < nthreads ; i++) {
            if (threads[i] == t) {
                System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
                // Zap dangling reference to the dead thread so that
                // the garbage collector will collect it.
                threads[nthreads] = null;
                break;
            }
        }
    }
}

移除線程

public void list() {
    list(System.out, 0);
}
void list(PrintStream out, int indent) {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        for (int j = 0 ; j < indent ; j++) {
            out.print(" ");
        }
        out.println(this);
        indent += 4;
        for (int i = 0 ; i < nthreads ; i++) {
            for (int j = 0 ; j < indent ; j++) {
                out.print(" ");
            }
            out.println(threads[i]);
        }
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
    }
    for (int i = 0 ; i < ngroupsSnapshot ; i++) {
        groupsSnapshot[i].list(out, indent);
    }
}

打印線程組列表信息

public void uncaughtException(Thread t, Throwable e) {
    if (parent != null) {
        parent.uncaughtException(t, e);
    } else {
        Thread.UncaughtExceptionHandler ueh =
            Thread.getDefaultUncaughtExceptionHandler();
        if (ueh != null) {
            ueh.uncaughtException(t, e);
        } else if (!(e instanceof ThreadDeath)) {
            System.err.print("Exception in thread \"" + t.getName() + "\" ");
            e.printStackTrace(System.err);
        }
    }
}

實現了未捕獲異常方法,優先調用當前線程的異常handler,沒有的話調用全局的默認異常handler,如果還沒有就執行e.printStackTrace

public String toString() {
    return getClass().getName() + "[name=" + getName() + ",maxpri=" + maxPriority + "]";
}

輸出相關信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM