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

Better interface for getting and counting threads and thread groups

    Details

    • Subcomponent:
    • CPU:
      generic, x86, sparc
    • OS:
      generic, solaris_1, solaris_2.5.1, windows_nt

      Description

      I have tried mightily to resist the urge to redesign as I document,
      but in the area of threads and groups I find the "enumerate" mess
      sufficiently weird that I would like to propose replacement functionality:

      Deprecate: enumerate, activeCount, activeGroupCount from ThreadGroup
      enumerate, activeCount from Thread

      To ThreadGroup add:
      int threadsCount()
      int allThreadsCount() // equals activeCount
      int groupsCount()
      int allGroupsCount() // equals activeGroupCount
      Thread[] threads()
      ThreadGroup[] groups()
      Thread[] allThreads()
      ThreadGroup[] allGroups()

      Eight methods, each of no arguments, with three orthogonal attributes:
      "threads" deals with threads, "groups" with thread groups
      "all" means a recursive search, otherwise just immediate ones
      "count" means returns an int, otherwise return an array of them

      James says he likes this.

      Here is the proposed code. Yes, I am aware that allThreads and allGroups
      may have execution time quadratic in the number of threads. I don't think
      it matters in practice---if it does, eventually we can substitute a more
      clever algorithm.

      --Guy


          public synchronized int threadsCount() {
      return nthreads;
          }

          public synchronized int allThreadsCount() {
      int n = nthreads;
      for (int i = 0 ; i < ngroups ; i++) {
      n += groups[i].activeCount();
      }
      return n;
          }

          public synchronized int groupsCount() {
      return ngroups;
          }

          public synchronized int allGroupsCount() {
      int n = ngroups;
      for (int i = 0 ; i < ngroups ; i++) {
      n += groups[i].activeGroupCount();
      }
      return n;
          }

          private Thread[] emptyThreadsArray = new Thread[0];

          public synchronized Thread[] threads() {
      if (nthreads == 0)
      return emptyThreadsArray;
      Thread[] result = new Thread[nthreads];
      System.arraycopy(threads, 0, result, 0, nthreads);
      return result;
          }

          public synchronized Thread[] allThreads() {
      Thread[] result = threads();
      for (int i = 0 ; i < ngroups ; i++) {
      Thread[] more = groups[i].allThreads();
      if (more.length != 0) {
      if (result.length == 0)
      result = more;
      else {
      Thread[] concat = new Thread[result.length + more.length];
      System.arraycopy(result, 0, concat, 0, result.length);
      System.arraycopy(more, 0, concat, result.length, more.length);
      result = concat;
      }
      }
      }
      return result;
          }

          private ThreadGroup[] emptyGroupsArray = new ThreadGroup[0];

          public synchronized ThreadGroup[] groups() {
      if (ngroups == 0)
      return emptyGroupsArray;
      ThreadGroup[] result = new ThreadGroup[ngroups];
      System.arraycopy(groups, 0, result, 0, ngroups);
      return result;
          }

          public synchronized ThreadGroup[] allGroups() {
      ThreadGroup[] result = groups();
      for (int i = 0 ; i < ngroups ; i++) {
      ThreadGroup[] more = groups[i].allGroups();
      if (more.length != 0) {
      if (result.length == 0)
      result = more;
      else {
      ThreadGroup[] concat = new ThreadGroup[result.length + more.length];
      System.arraycopy(result, 0, concat, 0, result.length);
      System.arraycopy(more, 0, concat, result.length, more.length);
      result = concat;
      }
      }
      }
      return result;
          }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jjb Josh Bloch
                Reporter:
                duke J. Duke (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: