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.