JDK-4082405 : (thread) Thread.enumerate() yields wrong result (it counts inactive threads)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: solaris_2.4
  • CPU: sparc
  • Submitted: 1997-09-29
  • Updated: 2005-09-19
  • Resolved: 2005-09-19
Related Reports
Relates :  
Relates :  
Relates :  
Description
Name: akC45999			Date: 09/29/97



The specification of Thread.enumerate(Thread[]) method says that
only active threads are taken into account. However, jdk12M seems 
to count inactive threads also. 

// test is derived from javasoft.sqe.tests.api.java.lang.Thread.enumerate0101

import java.io.PrintStream;

class enumerate0101t extends Thread {
  boolean act; // must stay active?
  ThreadGroup group;

  enumerate0101t(ThreadGroup group, boolean act) {
	super(group, String.valueOf(act));
	this.group=group;
	this.act=act;
  }

  public void run() {
	if (!act) { return;}
	synchronized(group) {
		try {
			group.wait();
		} catch (InterruptedException e) {
		}
	}
  }

} // end class enumerate0101t



public class enumerate0101 { //implements Test {

  public static void main(String args[]) {
	ThreadGroup group=new ThreadGroup("enumerate0101");
// create 1 active and 2 inactive threads in this group:
	Thread activeThrd, nonActiveThrd1, nonActiveThrd2;
	activeThrd=new enumerate0101t(group, true);
	activeThrd.start();
	nonActiveThrd1=new enumerate0101t(group, false);
	nonActiveThrd1.start();
	nonActiveThrd2=new enumerate0101t(group, false); // no start
	try {
		nonActiveThrd1.join();
	} catch (InterruptedException e) {
	}
// call Thread.enumerate():
	Thread tarray[]=new Thread[10];
	int cnt0=activeThrd.enumerate(tarray);
	System.err.println("activeThrd.enumerate()="+cnt0);
// make activeThrd to finish:
	synchronized(group) {
		group.notify();
	}
  }

}



Running the test:
novo37% javac enumerate0101.java
novo37% setenv CLASSPATH .
novo37% java enumerate0101
activeThrd.enumerate()=3


======================================================================

Comments
EVALUATION This was declared a bug in enumerate by a fix causing enumerate to only count threads in its group for which Thread.isAlive() returns true. This change was integrated into SE version 1.1.8_009. So this defect is *fixed* despite the fact that post-hoc process requires setting this change request to "closed/not not reproducible" (that is, it is "no longer reproducible" with all versions from 1.1.8_009 onward). In combination with the fact that a thread is removed from its group when it terminates, the meaning of "active" is better defined now, but still poorly documented. A note was added to CR 4189292 to be sure to improve ThreadGroup documentation in regard to the term "active." HOWEVER bug 4162694 is not fixed yet and constitutes a competing definition of "active."
19-09-2005

EVALUATION This bug is related to 4089701 (and 4189292). We should decide whether to change meaning of active, or to document the current one. ###@###.### 2002-11-08
08-11-2002

WORK AROUND We can add isAlive() detection during the thread enumeration. ###@###.### 1998-01-29
29-01-1998