- 总结与使用
- 1.extends 和 implement
- 2.构造方法
- ThreadGroup(Void unused, ThreadGroup parent, String name)
- 3.属性
- 4.方法
- native方法
- Java方法
- checkParentAccess(ThreadGroup)
- parentOf(ThreadGroup)
- checkAccess(): 权限检查
- activeCount():
- enumerate(Thread[] list,int n,boolean recurse):
- activeGroupCount():
- enumerate(ThreadGroup list[],int n, boolean recurse)
- stop(): Deprecated
- suspend(): Deprecated
- stopOrSuspend(boolean suspend):
- interrupt():
- resume(): Deprecated
- destroy():
- add(ThreadGroup g):
- remove(ThreadGroup g):
- addUnstarted():
- add(Thread t) :
- threadStartFailed(Thread t):
- threadTerminated(Thread t):
- remove():
- list():
- uncaughtException(Thread t ,Throwable e):
- allowThreadSuspension(boolean b):
- 5.内部类
总结与使用
除初始线程组,其他的线程组都有父线程组
1.extends 和 implement
implements Thread.UncaughtExceptionHandler;
线程未捕获异常抛出候的处理类
2.构造方法
无参构造 ThreadGroup()
private ThreadGroup() {
this.name = "system";
this.maxPriority = Thread.MAX_PRIORITY;
this.parent = null;
}
ThreadGroup(String)
public ThreadGroup(String name) {
this(Thread.currentThread().getThreadGroup(), name);
}
ThreadGroup(ThreadGroup parent, String name)
public ThreadGroup(ThreadGroup parent, String name) {
this(checkParentAccess(parent), parent, name);
}
ThreadGroup(Void unused, ThreadGroup parent, String name)
// Void unused , 父线程组需要进行安全检查, Void 方法
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.属性
【destroyed】: 是否被销毁
【daemon】: 是否守护线程组,守护线程组在线程组最后一个线程停止或者最后一个子线程组被移除时会自动销毁
【parent】:父线程组,final 修饰
【name】:线程组名称
【maxPriority】:线程组内最高优先级
【vmAllowSuspension】:vm是否允许中断
【nUnstartedThreads】:未启动的线程数
【nthreads】:线程组中的线程数
【threads】:线程组中的线程
【ngroups】:线程组数目
【groups】:线程组中的子线程组
4.方法
native方法
Java方法
checkParentAccess(ThreadGroup)
检查当前线程是否有修改父线程组的权限
private static Void checkParentAccess(ThreadGroup parent) {
parent.checkAccess();
return null;
}
parentOf(ThreadGroup)
public final boolean parentOf(ThreadGroup g) {
for (; g != null ; g = g.parent) {
if (g == this) {
return true;
}
}
return false;
}
checkAccess(): 权限检查
activeCount():
查询线程组中的活动线程数量,返回的值并不准确,仅作为参考,线程组的线程时刻在变化。
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;
}
enumerate(Thread[] list,int n,boolean recurse):
将当前线程组的所有活动线程复制到list数组中,数组写满后停止,
n表示从list数组中的第n个位置开始写入线程。
recurse表示将当前线程组中的子线程组中线程也写入list。
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;
}
//循环位置从n开始 如果n已经到了数组末尾,那么nt=0,不进入循环写数据
int nt = nthreads;
if (nt > list.length - n) {
nt = list.length - n;
}
for (int i = 0; i < nt; i++) {
//如果线程存活,写入list
if (threads[i].isAlive()) {
list[n++] = threads[i];
}
}
//如果需要递归子线程组,那么写入子线程组快照
if (recurse) {
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
}
//将子线程组的数据递归写入list中
if (recurse) {
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
n = groupsSnapshot[i].enumerate(list, n, true);
}
}
//返回list中
return n;
}
activeGroupCount():
存活的线程组数量,生成快照,从快照中统计存活的线程组数量
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;
}
enumerate(ThreadGroup list[],int n, boolean recurse)
参照Thread的enumerate方法
public int enumerate(ThreadGroup list[]) {
checkAccess();
return enumerate(list, 0, true);
}
public int enumerate(ThreadGroup list[]) {
checkAccess();
return enumerate(list, 0, true);
}
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;
}
stop(): Deprecated
停止线程组内的所有线程, 弃用原因是调用了线程的stop方法,参看Thread类的stop方法。
@Deprecated
public final void stop() {
if (stopOrSuspend(false))
Thread.currentThread().stop();
}
suspend(): Deprecated
挂起线程组所有线程
@Deprecated
@SuppressWarnings("deprecation")
public final void suspend() {
if (stopOrSuspend(true))
Thread.currentThread().suspend();
}
stopOrSuspend(boolean suspend):
停止或者挂起 ,suspend false 表示停止所有线程,true表示挂起所有线程 , 停止的线程包括线程组子组里得所有线程,递归调用
@SuppressWarnings("deprecation")
private boolean stopOrSuspend(boolean suspend) {
boolean suicide = false;
Thread us = Thread.currentThread();
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot = null;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
if (threads[i]==us)
suicide = true;
else if (suspend)
threads[i].suspend();
else
threads[i].stop();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++)
suicide = groupsSnapshot[i].stopOrSuspend(suspend) || suicide;
return suicide;
}
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();
}
}
resume(): Deprecated
恢复所有的线程
@Deprecated
@SuppressWarnings("deprecation")
public final void resume() {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
threads[i].resume();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].resume();
}
}
destroy():
销毁线程组
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;
}
//为什么需要判断paraent!=null ,因为除了初始线程组外,其他线程组都有parent,也就是说,初始线程组是不能销毁的
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);
}
}
add(ThreadGroup g):
添加线程组到组内
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++;
}
}
remove(ThreadGroup g):
移除线程组
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();
}
}
}
addUnstarted():
自增长未启动线程数量
void addUnstarted() {
synchronized(this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
nUnstartedThreads++;
}
}
add(Thread t) :
添加线程到线程组中
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--;
}
}
threadStartFailed(Thread t):
线程启动失败通知
void threadStartFailed(Thread t) {
synchronized(this) {
remove(t);
nUnstartedThreads++;
}
}
threadTerminated(Thread t):
线程终止,通知
void threadTerminated(Thread t) {
synchronized (this) {
remove(t);
if (nthreads == 0) {
notifyAll();
}
//如果是守护线程,需要检查,是否符合销毁线程组的条件
if (daemon && (nthreads == 0) &&(nUnstartedThreads == 0) && (ngroups == 0)){
destroy();
}
}
}
remove():
移除线程组的线程
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);
threads[nthreads] = null;
break;
}
}
}
}
list():
列举线程组成员
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);
}
}
uncaughtException(Thread t ,Throwable e):
线程组内线程异常未捕获时的处理方法
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);
}
}
}
allowThreadSuspension(boolean b):
what is lowmem?
@Deprecated
public boolean allowThreadSuspension(boolean b) {
this.vmAllowSuspension = b;
if (!b) {
VM.unsuspendSomeThreads();
}
return true;
}