Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4412854

(thread) Thread.activeCount() returns wrong value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.1, 1.3.0
    • core-libs
    • generic, sparc
    • generic, solaris_7

      Name: yyT116575 Date: 02/07/2001


      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 Microsoft
      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 Microsoft
      Windows 95/98/NT.
      (Review ID: 116515)
      ======================================================================

            psoper Pete Soper (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: