一、源码
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 + "]"; }
输出相关信息