home *** CD-ROM | disk | FTP | other *** search
- package java.lang;
-
- import java.io.PrintStream;
-
- public class ThreadGroup {
- ThreadGroup parent;
- String name;
- int maxPriority;
- boolean destroyed;
- boolean daemon;
- int nthreads;
- Thread[] threads;
- int ngroups;
- ThreadGroup[] groups;
-
- private ThreadGroup() {
- this.name = "system";
- this.maxPriority = 10;
- }
-
- public ThreadGroup(String name) {
- this(Thread.currentThread().getThreadGroup(), name);
- }
-
- public ThreadGroup(ThreadGroup parent, String name) {
- if (parent == null) {
- throw new NullPointerException();
- } else {
- parent.checkAccess(1);
- this.name = name;
- this.maxPriority = parent.maxPriority;
- this.daemon = parent.daemon;
- this.parent = parent;
- parent.add(this);
- }
- }
-
- public final String getName() {
- return this.name;
- }
-
- public final ThreadGroup getParent() {
- return this.parent;
- }
-
- public final int getMaxPriority() {
- return this.maxPriority;
- }
-
- public final boolean isDaemon() {
- return this.daemon;
- }
-
- public final void setDaemon(boolean daemon) {
- SecurityManager.checksAccess(this, 1);
- this.daemon = daemon;
- }
-
- public final synchronized void setMaxPriority(int pri) {
- SecurityManager.checksAccess(this, 1);
- if (pri < 1) {
- this.maxPriority = 1;
- } else if (pri < this.maxPriority) {
- this.maxPriority = pri;
- }
-
- for(int i = 0; i < this.ngroups; ++i) {
- this.groups[i].setMaxPriority(pri);
- }
-
- }
-
- public final boolean parentOf(ThreadGroup g) {
- while(g != null) {
- if (g == this) {
- return true;
- }
-
- g = g.parent;
- }
-
- return false;
- }
-
- public final void checkAccess(int caller_depth) {
- SecurityManager.checksAccess(this, caller_depth + 1);
- }
-
- public synchronized int activeCount() {
- if (this.destroyed) {
- return 0;
- } else {
- ThreadGroup group = this.enumeratableThreadGroup();
- return group == null ? 0 : group.activeCount0();
- }
- }
-
- private int activeCount0() {
- if (this.destroyed) {
- return 0;
- } else {
- int n = this.nthreads;
-
- for(int i = 0; i < this.ngroups; ++i) {
- n += this.groups[i].activeCount0();
- }
-
- return n;
- }
- }
-
- public int enumerate(Thread[] list) {
- return this.enumerate(list, true);
- }
-
- public int enumerate(Thread[] list, boolean recurse) {
- ThreadGroup legal_group = this.enumeratableThreadGroup();
- return legal_group != null && (recurse || this == legal_group) ? legal_group.enumerate((Thread[])list, 0, recurse) : 0;
- }
-
- private synchronized int enumerate(Thread[] list, int n, boolean recurse) {
- if (this.destroyed) {
- return 0;
- } else {
- int nt = this.nthreads;
- if (nt > list.length - n) {
- nt = list.length - n;
- }
-
- if (nt > 0) {
- System.arraycopy(this.threads, 0, list, n, nt);
- n += nt;
- }
-
- if (recurse) {
- for(int i = 0; i < this.ngroups; ++i) {
- n = this.groups[i].enumerate(list, n, true);
- }
- }
-
- return n;
- }
- }
-
- public synchronized int activeGroupCount() {
- if (this.destroyed) {
- return 0;
- } else {
- ThreadGroup legal_group = this.enumeratableThreadGroup();
- if (legal_group == null) {
- return 0;
- } else {
- int n = 0;
-
- for(ThreadGroup parents = legal_group; parents != this; parents = parents.parent) {
- ++n;
- }
-
- return n + legal_group.activeGroupCount0();
- }
- }
- }
-
- private int activeGroupCount0() {
- if (this.destroyed) {
- return 0;
- } else {
- int n = this.ngroups;
-
- for(int i = 0; i < this.ngroups; ++i) {
- n += this.groups[i].activeGroupCount0();
- }
-
- return n;
- }
- }
-
- public int enumerate(ThreadGroup[] list) {
- return this.enumerate(list, true);
- }
-
- public synchronized int enumerate(ThreadGroup[] list, boolean recurse) {
- ThreadGroup legal_group = this.enumeratableThreadGroup();
- if (legal_group == null) {
- return 0;
- } else {
- int n = 0;
- n = this.enumerateChildren(legal_group, list, recurse);
- return !recurse && n > 0 ? n : legal_group.enumerate(list, n, recurse);
- }
- }
-
- private int enumerateChildren(ThreadGroup child, ThreadGroup[] list, boolean recurse) {
- if (this == child) {
- return 0;
- } else {
- ThreadGroup parent = child.parent;
- int n = this.enumerateChildren(parent, list, recurse);
- if (n < list.length && (n == 0 || recurse)) {
- list[n++] = child;
- }
-
- return n;
- }
- }
-
- private synchronized int enumerate(ThreadGroup[] list, int n, boolean recurse) {
- if (this.destroyed) {
- return 0;
- } else {
- int ng = this.ngroups;
- if (ng > list.length - n) {
- ng = list.length - n;
- }
-
- if (ng > 0) {
- System.arraycopy(this.groups, 0, list, n, ng);
- n += ng;
- }
-
- if (recurse) {
- for(int i = 0; i < this.ngroups; ++i) {
- n = this.groups[i].enumerate(list, n, true);
- }
- }
-
- return n;
- }
- }
-
- public final synchronized void stop() {
- SecurityManager.checksAccess(this, 1);
-
- for(int i = 0; i < this.ngroups; ++i) {
- this.groups[i].stop();
- }
-
- for(int i = 0; i < this.nthreads; ++i) {
- this.threads[i].stop();
- }
-
- }
-
- public final synchronized void suspend() {
- SecurityManager.checksAccess(this, 1);
-
- for(int i = 0; i < this.ngroups; ++i) {
- this.groups[i].suspend();
- }
-
- for(int i = 0; i < this.nthreads; ++i) {
- this.threads[i].suspend();
- }
-
- }
-
- public final synchronized void resume() {
- SecurityManager.checksAccess(this, 1);
-
- for(int i = 0; i < this.ngroups; ++i) {
- this.groups[i].resume();
- }
-
- for(int i = 0; i < this.nthreads; ++i) {
- this.threads[i].resume();
- }
-
- }
-
- public final synchronized void destroy() {
- SecurityManager.checksAccess(this, 1);
- if (!this.destroyed && this.nthreads <= 0) {
- while(this.ngroups > 0) {
- this.groups[0].destroy();
- }
-
- if (this.parent != null) {
- this.destroyed = true;
- this.groups = null;
- this.threads = null;
- this.parent.remove(this);
- }
-
- } else {
- throw new IllegalThreadStateException();
- }
- }
-
- private final synchronized void add(ThreadGroup g) {
- if (this.destroyed) {
- throw new IllegalThreadStateException();
- } else {
- SecurityManager.setScopePermission();
- if (this.groups == null) {
- this.groups = new ThreadGroup[4];
- } else if (this.ngroups == this.groups.length) {
- ThreadGroup[] newgroups = new ThreadGroup[this.ngroups * 2];
- System.arraycopy(this.groups, 0, newgroups, 0, this.ngroups);
- this.groups = newgroups;
- }
-
- this.groups[this.ngroups] = g;
- ++this.ngroups;
- }
- }
-
- private synchronized void remove(ThreadGroup g) {
- if (!this.destroyed) {
- for(int i = 0; i < this.ngroups; ++i) {
- if (this.groups[i] == g) {
- System.arraycopy(this.groups, i + 1, this.groups, i, --this.ngroups - i);
- this.groups[this.ngroups] = null;
- break;
- }
- }
-
- if (this.nthreads == 0) {
- this.notifyAll();
- }
-
- if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
- SecurityManager.setScopePermission();
- this.destroy();
- }
-
- }
- }
-
- synchronized void add(Thread t) {
- if (this.destroyed) {
- throw new IllegalThreadStateException();
- } else {
- if (this.threads == null) {
- this.threads = new Thread[4];
- } else if (this.nthreads == this.threads.length) {
- Thread[] newthreads = new Thread[this.nthreads * 2];
- System.arraycopy(this.threads, 0, newthreads, 0, this.nthreads);
- this.threads = newthreads;
- }
-
- this.threads[this.nthreads] = t;
- ++this.nthreads;
- }
- }
-
- synchronized void remove(Thread t) {
- if (!this.destroyed) {
- for(int i = 0; i < this.nthreads; ++i) {
- if (this.threads[i] == t) {
- System.arraycopy(this.threads, i + 1, this.threads, i, --this.nthreads - i);
- this.threads[this.nthreads] = null;
- break;
- }
- }
-
- if (this.nthreads == 0) {
- this.notifyAll();
- }
-
- if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
- this.destroy();
- }
-
- }
- }
-
- public synchronized void list() {
- this.list(System.out, 0);
- }
-
- void list(PrintStream out, int indent) {
- for(int j = 0; j < indent; ++j) {
- out.print(" ");
- }
-
- out.println(this);
- indent += 4;
-
- for(int i = 0; i < this.nthreads; ++i) {
- for(int j = 0; j < indent; ++j) {
- out.print(" ");
- }
-
- out.println(this.threads[i]);
- }
-
- for(int i = 0; i < this.ngroups; ++i) {
- this.groups[i].list(out, indent);
- }
-
- }
-
- public void uncaughtException(Thread t, Throwable e) {
- if (this.parent != null) {
- this.parent.uncaughtException(t, e);
- } else {
- if (!(e instanceof ThreadDeath)) {
- e.printStackTrace(System.err);
- }
-
- }
- }
-
- public String toString() {
- return this.getClass().getName() + "[name=" + this.getName() + ",maxpri=" + this.maxPriority + "]";
- }
-
- private ThreadGroup enumeratableThreadGroup() {
- ThreadGroup direct_parent = Thread.currentThread().getThreadGroup();
- if (direct_parent != this && !direct_parent.parentOf(this)) {
- return this.parentOf(direct_parent) ? direct_parent : null;
- } else {
- return this;
- }
- }
- }
-