I have tried mightily to resist the urge to redesign as I document,
but in the area of threads and groups I find the "enumerate" mess
sufficiently weird that I would like to propose replacement functionality:
Deprecate: enumerate, activeCount, activeGroupCount from ThreadGroup
enumerate, activeCount from Thread
To ThreadGroup add:
int threadsCount()
int allThreadsCount() // equals activeCount
int groupsCount()
int allGroupsCount() // equals activeGroupCount
Thread[] threads()
ThreadGroup[] groups()
Thread[] allThreads()
ThreadGroup[] allGroups()
Eight methods, each of no arguments, with three orthogonal attributes:
"threads" deals with threads, "groups" with thread groups
"all" means a recursive search, otherwise just immediate ones
"count" means returns an int, otherwise return an array of them
James says he likes this.
Here is the proposed code. Yes, I am aware that allThreads and allGroups
may have execution time quadratic in the number of threads. I don't think
it matters in practice---if it does, eventually we can substitute a more
clever algorithm.
--Guy
public synchronized int threadsCount() {
return nthreads;
}
public synchronized int allThreadsCount() {
int n = nthreads;
for (int i = 0 ; i < ngroups ; i++) {
n += groups[i].activeCount();
}
return n;
}
public synchronized int groupsCount() {
return ngroups;
}
public synchronized int allGroupsCount() {
int n = ngroups;
for (int i = 0 ; i < ngroups ; i++) {
n += groups[i].activeGroupCount();
}
return n;
}
private Thread[] emptyThreadsArray = new Thread[0];
public synchronized Thread[] threads() {
if (nthreads == 0)
return emptyThreadsArray;
Thread[] result = new Thread[nthreads];
System.arraycopy(threads, 0, result, 0, nthreads);
return result;
}
public synchronized Thread[] allThreads() {
Thread[] result = threads();
for (int i = 0 ; i < ngroups ; i++) {
Thread[] more = groups[i].allThreads();
if (more.length != 0) {
if (result.length == 0)
result = more;
else {
Thread[] concat = new Thread[result.length + more.length];
System.arraycopy(result, 0, concat, 0, result.length);
System.arraycopy(more, 0, concat, result.length, more.length);
result = concat;
}
}
}
return result;
}
private ThreadGroup[] emptyGroupsArray = new ThreadGroup[0];
public synchronized ThreadGroup[] groups() {
if (ngroups == 0)
return emptyGroupsArray;
ThreadGroup[] result = new ThreadGroup[ngroups];
System.arraycopy(groups, 0, result, 0, ngroups);
return result;
}
public synchronized ThreadGroup[] allGroups() {
ThreadGroup[] result = groups();
for (int i = 0 ; i < ngroups ; i++) {
ThreadGroup[] more = groups[i].allGroups();
if (more.length != 0) {
if (result.length == 0)
result = more;
else {
ThreadGroup[] concat = new ThreadGroup[result.length + more.length];
System.arraycopy(result, 0, concat, 0, result.length);
System.arraycopy(more, 0, concat, result.length, more.length);
result = concat;
}
}
}
return result;
}
but in the area of threads and groups I find the "enumerate" mess
sufficiently weird that I would like to propose replacement functionality:
Deprecate: enumerate, activeCount, activeGroupCount from ThreadGroup
enumerate, activeCount from Thread
To ThreadGroup add:
int threadsCount()
int allThreadsCount() // equals activeCount
int groupsCount()
int allGroupsCount() // equals activeGroupCount
Thread[] threads()
ThreadGroup[] groups()
Thread[] allThreads()
ThreadGroup[] allGroups()
Eight methods, each of no arguments, with three orthogonal attributes:
"threads" deals with threads, "groups" with thread groups
"all" means a recursive search, otherwise just immediate ones
"count" means returns an int, otherwise return an array of them
James says he likes this.
Here is the proposed code. Yes, I am aware that allThreads and allGroups
may have execution time quadratic in the number of threads. I don't think
it matters in practice---if it does, eventually we can substitute a more
clever algorithm.
--Guy
public synchronized int threadsCount() {
return nthreads;
}
public synchronized int allThreadsCount() {
int n = nthreads;
for (int i = 0 ; i < ngroups ; i++) {
n += groups[i].activeCount();
}
return n;
}
public synchronized int groupsCount() {
return ngroups;
}
public synchronized int allGroupsCount() {
int n = ngroups;
for (int i = 0 ; i < ngroups ; i++) {
n += groups[i].activeGroupCount();
}
return n;
}
private Thread[] emptyThreadsArray = new Thread[0];
public synchronized Thread[] threads() {
if (nthreads == 0)
return emptyThreadsArray;
Thread[] result = new Thread[nthreads];
System.arraycopy(threads, 0, result, 0, nthreads);
return result;
}
public synchronized Thread[] allThreads() {
Thread[] result = threads();
for (int i = 0 ; i < ngroups ; i++) {
Thread[] more = groups[i].allThreads();
if (more.length != 0) {
if (result.length == 0)
result = more;
else {
Thread[] concat = new Thread[result.length + more.length];
System.arraycopy(result, 0, concat, 0, result.length);
System.arraycopy(more, 0, concat, result.length, more.length);
result = concat;
}
}
}
return result;
}
private ThreadGroup[] emptyGroupsArray = new ThreadGroup[0];
public synchronized ThreadGroup[] groups() {
if (ngroups == 0)
return emptyGroupsArray;
ThreadGroup[] result = new ThreadGroup[ngroups];
System.arraycopy(groups, 0, result, 0, ngroups);
return result;
}
public synchronized ThreadGroup[] allGroups() {
ThreadGroup[] result = groups();
for (int i = 0 ; i < ngroups ; i++) {
ThreadGroup[] more = groups[i].allGroups();
if (more.length != 0) {
if (result.length == 0)
result = more;
else {
ThreadGroup[] concat = new ThreadGroup[result.length + more.length];
System.arraycopy(result, 0, concat, 0, result.length);
System.arraycopy(more, 0, concat, result.length, more.length);
result = concat;
}
}
}
return result;
}
- duplicates
-
JDK-4083644 Thread and ThreadGroup don't implement/deprecate according to JLS 20.20
- Closed
-
JDK-4217311 int ThreadGroup.enumerate(Thread[]) should be Thread[] ThreadGroup.getChildTh...
- Closed
-
JDK-4097747 No easy way to get all threads in a thread group
- Closed
- relates to
-
JDK-4189292 (thread spec) ThreadGroup and Thread need better and tighter specs
- Closed