corda/classpath/java/lang/ThreadGroup.java
Joel Dice c1c9d2111b remove GNU Classpath and Apache Harmony compatibility code
Rather than try to support mixing Avian's core classes with those of
an external class library -- which necessitates adding a lot of stub
methods which throw UnsupportedOperationExceptions, among other
comprimises -- we're looking to support such external class libraries
in their unmodified forms.  The latter strategy has already proven
successful with OpenJDK's class library.  Thus, this commit removes
the stub methods, etc., which not only cleans up the code but avoids
misleading application developers as to what classes and methods
Avian's built-in class library supports.
2010-09-27 15:58:02 -06:00

118 lines
2.8 KiB
Java

/* Copyright (c) 2009, 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;
}
}