Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4412854
Votes 4
Synopsis (thread) Thread.activeCount() returns wrong value
Category java:classes_lang
Reported Against 1.3 , 1.2.1
Release Fixed
State 11-Closed, duplicate of 4089701, bug
Priority: 4-Low
Related Bugs 4229558 , 4253584
Submit Date 07-FEB-2001
Description


java version "1.3.0"
Java (TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

Two Problems with activeCount(), i.e. a class methods of java.lang.Thread
-------------------------------------------------------------------------

According to the "JavaTM 2 Platform, Standard Edition, v 1.3 - API
Specification":
- activeCount() returns the current number of ACTIVE threads in this thread's
thread group;
- enumerate() copies into the specified array every ACTIVE thread in this
thread's thread group and its subgroups, and returns the number of threads put
into the array.
 
Problem 1: activeCount() returns MORE than the number of active threads, i.e.
it returns the TOTAL number of ALL threads, including the threads that have not
been started yet!
Problem 2: sometimes(?), activeCount() even returns a wrong value, i.e. a
number EXCEEDING the total number of all threads!

The following application illustrates these two problems:

class MyClass extends Thread {
  static boolean stopThread = false;
  static void main(String args[]) {
    // part 1:
    int t = activeCount();
    Thread[] threadList = new Thread[10];
    int a = enumerate(threadList);
    System.out.println("(1) TOTAL # of threads = " + t
        + ", # of ACTIVE threads = " + a + ",\n    thread names =");
    for (int i = 0; i < a; i++) {
      System.out.println("        " + (i+1) + ": " + threadList[i].getName());
    }

    // part 2:
    MyClass threadDriver = new MyClass();
    Thread t1 = new Thread(threadDriver);
    Thread t2 = new Thread(threadDriver);
    t = activeCount();
    a = enumerate(threadList);
    System.out.println("(2) TOTAL # of threads = " + t
        + ", # of ACTIVE threads = " + a + ",\n    thread names =");
    for (int i = 0; i < a; i++) {
      System.out.println("        " + (i+1) + ": " + threadList[i].getName());
    }

    // part 3:
    t1.start();
    t2.start();
    t = activeCount();
    a = enumerate(threadList);
    System.out.println("(3) TOTAL # of threads = " + t
        + ", # of ACTIVE threads = " + a + ",\n    thread names =");
    for (int i = 0; i < a; i++) {
      System.out.println("        " + (i+1) + ": " + threadList[i].getName());
    }
    // end:
    stopThread = true;
  }
  public void run() {
    while (!stopThread) {
    }
  }
}

The program has been compiled and executed on Windows NT Version 4.0 and on
Windows 95 OEM Service Release 2, using the Java 2 SDK 1.3 for  customer 
Windows 95/98/NT.

On both platforms the programs displays:

(1) TOTAL # of threads = 3, # of ACTIVE threads = 3,
    thread names =
        1: main
        2: SymcJIT-LazyCompilation-0
        3: SymcJIT-LazyCompilation-PA
(2) TOTAL # of threads = 6, # of ACTIVE threads = 3,
    thread names =
        1: main
        2: SymcJIT-LazyCompilation-0
        3: SymcJIT-LazyCompilation-PA
(3) TOTAL # of threads = 6, # of ACTIVE threads = 5,
    thread names =
        1: main
        2: SymcJIT-LazyCompilation-0
        3: SymcJIT-LazyCompilation-PA
        4: Thread-1
        5: Thread-2

Output (1): O.K.
Output (2): activeCount() returns 6 (instead of 3) !!!
Output (3): activeCount() returns 6 (instead of 5) !!!

I encountered the same two problems when using the Java 2 SDK 1.2 for  customer 
Windows 95/98/NT.
(Review ID: 116515) 
======================================================================
Posted Date : 2005-10-24 19:27:38.0
Work Around
N/A
Evaluation
Verified both problems corrected but could only determine the one error was corrected as 4089701. Suspect the other problem (counting threads the app didn't create) was a VM fix. But both problems were corrected in 1.5.0 FCS.
Posted Date : 2005-10-24 19:27:38.0
Comments
  
  Include a link with my name & email   

Submitted On 10-JAN-2003
DaveLaw
Bug is also present in 1.4.1_01



PLEASE NOTE: JDK6 is formerly known as Project Mustang