JDK-1243764 : Better interface for getting and counting threads and thread groups
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.0,1.1.6,1.2.0
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS:
    generic,solaris_1,solaris_2.5.1,windows_nt generic,solaris_1,solaris_2.5.1,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1996-03-28
  • Updated: 2021-11-09
  • Resolved: 2003-06-23
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
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;
    }


Comments
PUBLIC COMMENTS Better interface for getting and counting threads and thread groups
10-06-2004

EVALUATION Thread group is no longer under active development. As per Item 53 in Bloch's "Effective Java," programmers are discouraged from using this class in new code. ###@###.### 2003-06-23
23-06-2003