示例#1
0
  private void showDefaultInfo(ThreadGroup grp) {
    TreeSet<Thread> threadSet =
        new TreeSet<Thread>(
            new Comparator<Thread>() {
              @Override
              public int compare(Thread t1, Thread t2) {
                return Long.valueOf(t1.getId()).compareTo(t2.getId());
              }
            });
    findThreads(grp, threadSet);

    PrintWriter out = getOutput().getPrintWriter();
    for (final Thread thread : threadSet) {
      VmThread vmThread =
          AccessController.doPrivileged(
              new PrivilegedAction<VmThread>() {
                public VmThread run() {
                  return ThreadHelper.getVmThread(thread);
                }
              });
      out.println(
          " "
              + thread.getId()
              + SEPARATOR
              + thread.getName()
              + SEPARATOR
              + thread.getPriority()
              + SEPARATOR
              + vmThread.getThreadStateName());
    }
  }
示例#2
0
  /**
   * Traverse the ThreadGroups threads and its child ThreadGroups printing information for each
   * thread found. If 'threadName' is non-null, only print information for the thread that matches
   * the name.
   *
   * @param grp the ThreadGroup to traverse
   * @param out the destination for output
   * @param threadName if non-null, only display this thread.
   */
  private void showThreads(ThreadGroup grp, PrintWriter out, String threadName) {
    if (threadName == null) {
      out.println(GROUP + grp.getName());
    }

    final int max = grp.activeCount() * 2;
    final Thread[] ts = new Thread[max];
    grp.enumerate(ts);

    for (int i = 0; i < max; i++) {
      final Thread t = ts[i];
      if (t != null) {
        if ((threadName == null) || threadName.equals(t.getName())) {
          VmThread vmThread =
              AccessController.doPrivileged(
                  new PrivilegedAction<VmThread>() {
                    public VmThread run() {
                      return ThreadHelper.getVmThread(t);
                    }
                  });
          out.println(
              SLASH_T
                  + t.getId()
                  + SEPARATOR
                  + t.getName()
                  + SEPARATOR
                  + t.getPriority()
                  + SEPARATOR
                  + vmThread.getThreadStateName());
          if (threadName != null) {
            final Object[] trace = VmThread.getStackTrace(vmThread);
            final int traceLen = trace.length;
            out.println(SLASH_T + SLASH_T + TRACE);
            for (int k = 0; k < traceLen; k++) {
              out.println(SLASH_T + SLASH_T + trace[k]);
            }
            return;
          }
        }
      }
    }

    final int gmax = grp.activeGroupCount() * 2;
    final ThreadGroup[] tgs = new ThreadGroup[gmax];
    grp.enumerate(tgs);
    for (int i = 0; i < gmax; i++) {
      final ThreadGroup tg = tgs[i];
      if (tg != null) {
        showThreads(tg, out, threadName);
      }
    }
  }