JDK-4089701 : (thread) ThreadGroup.activeCount() counts non-started threads
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.0,1.2.1,1.3.0,1.4.0,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS:
    generic,solaris_2.4,solaris_7,solaris_8,windows_95 generic,solaris_2.4,solaris_7,solaris_8,windows_95
  • CPU: generic,x86,sparc
  • Submitted: 1997-10-30
  • Updated: 2004-03-29
  • Resolved: 2003-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b32Fixed
Related Reports
Duplicate :  
Duplicate :  
Description

Name: akC45999			Date: 10/30/97



Specification of class java.lang.ThreadGroup reads:

 public static int activeCount()

    Returns the current number of active threads in this thread group. 

Sun's JDK 1.1 ... 1.2q activeCount() counts also the threads which was not
started yet (see the test).

[The spec doesn't explicitly states that a thred which is not started is not active,
but does it implicitly, in the spec of the ThreadGroup.setDaemon() method].

------------------------------------- file activeCount0101.java
// simplified version of the test
// javasoft.sqe.tests.api.java.lang.ThreadGroup.activeCount0101;

import java.io.PrintStream;

class activeCount0101r implements Runnable {
  boolean fin=false;

  public synchronized void run() {
	if (fin) return;
	try {
		wait();
	} catch (InterruptedException e) {
	}
  }

  synchronized void finish() {
	fin=true;
	notifyAll();
  }

} // end class activeCount0101r


class activeCount0101 extends ThreadGroup {

  static final int activeNum=1; // active threads in each group

  activeCount0101() {
	super("activeCount0101");
  }

  public void test() {
	activeCount0101r event=new activeCount0101r();
	Thread activeThrd=new Thread(this, event, "activeCount0101_a")
		, nonActiveThrd=new Thread(this, event, "activeCount0101_n")
	;
	activeThrd.start();

	int cnt=activeCount();
	event.finish();
	if (cnt != activeNum)
		System.out.println("activeNum:"+activeNum+" activeCount():"+cnt);

  }

  public static void main(String args[]) {
	activeCount0101 group=new activeCount0101();
	group.test();
  }

} // end class activeCount0101

------------------------------------- end of file activeCount0101.java

Running the test:
novo64% javac activeCount0101.java
novo64% setenv CLASSPATH .
novo64% java activeCount0101
activeNum:1 activeCount():2
novo64% 

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

	In addition to ThreadGroup, I found the same problem with the
activeCount() method in the Thread class.  I attached two files - 
NeverStartedThreads.java and NeverStartedThreads.pass , a test and its
golden file - to illustrate the problem in more detail.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b32 tiger-beta VERIFIED IN: tiger-beta2
14-06-2004

EVALUATION It's not clear whether we should change the meaning of "active" at this point. If we elect to leave things as they stand, we should document it. ###@###.### 2002-11-08 Thread changes in Tiger make activeCount ignore unstarted Threads. activeCount() and enumerate will report only started threads. ###@###.### 2003-11-24
08-11-2002