corda/classpath/java/lang/ThreadGroup.java
Joel Dice 87b02eb949 update copyright years
Previously, I used a shell script to extract modification date ranges
from the Git history, but that was complicated and unreliable, so now
every file just gets the same year range in its copyright header.  If
someone needs to know when a specific file was modified and by whom,
they can look at the Git history themselves; no need to include it
redundantly in the header.
2013-07-02 20:52:38 -06:00

118 lines
2.8 KiB
Java

/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.lang;
import avian.Cell;
public class ThreadGroup implements Thread.UncaughtExceptionHandler {
private final ThreadGroup parent;
private final String name;
private Cell<ThreadGroup> subgroups;
public ThreadGroup(ThreadGroup parent, String name) {
this.parent = parent;
this.name = name;
synchronized (parent) {
parent.subgroups = new Cell(this, subgroups);
}
}
public ThreadGroup(String name) {
this(Thread.currentThread().getThreadGroup(), name);
}
public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler deh
= Thread.getDefaultUncaughtExceptionHandler();
if (deh != null) {
deh.uncaughtException(t, e);
} else if (! (e instanceof ThreadDeath)) {
e.printStackTrace();
}
}
}
public ThreadGroup getParent() {
return parent;
}
public String getName() {
return name;
}
public int activeCount() {
int allCount = Thread.activeCount();
Thread[] all = new Thread[allCount];
allCount = Thread.enumerate(all);
int count = 0;
for (int i = 0; i < allCount; ++i) {
if (parentOf(all[i].getThreadGroup())) {
++ count;
}
}
return count;
}
public int enumerate(Thread[] threads) {
return enumerate(threads, true);
}
public int enumerate(Thread[] threads, boolean recurse) {
int allCount = Thread.activeCount();
Thread[] all = new Thread[allCount];
allCount = Thread.enumerate(all);
int count = 0;
for (int i = 0; i < allCount && count < threads.length; ++i) {
Thread t = all[i];
ThreadGroup g = t.getThreadGroup();
if (g == this || (recurse && parentOf(g))) {
threads[count++] = t;
}
}
return count;
}
public boolean parentOf(ThreadGroup g) {
for (; g != null; g = g.parent) {
if (g == this) {
return true;
}
}
return false;
}
public int enumerate(ThreadGroup[] groups, boolean recurse) {
return enumerate(groups, recurse, 0);
}
private int enumerate(ThreadGroup[] groups, boolean recurse, int count) {
for (Cell<ThreadGroup> c = subgroups; c != null && count < groups.length;
c = c.next)
{
ThreadGroup g = c.value;
groups[count++] = g;
if (recurse) {
count = g.enumerate(groups, true, count);
}
}
return count;
}
}