ThreadGroup

Posted ctxsdhy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}
确认权限

  

 

以上是关于ThreadGroup的主要内容,如果未能解决你的问题,请参考以下文章

线程组ThreadGroup

java ThreadGroup源码分析

010-ThreadGroup线程组

使用ThreadGroup模拟线程池

java ThreadGroup 作用 方法解析(转)

java多线程-ThreadGroup