【问题标题】:How to check the number of currently running threads in Java?如何检查Java中当前正在运行的线程数?
【发布时间】:2012-07-18 08:10:44
【问题描述】:

我正在寻找一种方法来查看当前正在运行的线程数

  1. 首先通过 Windows
  2. 以编程方式

【问题讨论】:

  • 你的意思是编程吗?
  • 我是处理多线程的新手。我有一个使用大小为 10 的线程池的 java 程序。当程序运行时,如何检查我的程序当前正在运行多少线程?我需要一种方法来通过 Windows 以及通过程序本身打印出当时正在运行的线程数。

标签: java multithreading


【解决方案1】:

这将为您提供 VM 中的线程总数:

int nbThreads =  Thread.getAllStackTraces().keySet().size();

现在,如果您希望所有线程当前都在执行,您可以这样做:

int nbRunning = 0;
for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getState()==Thread.State.RUNNABLE) nbRunning++;
}

这里列举了可能的状态:Thread.State javadoc

如果您想通过 Windows 工具而不是编程方式查看正在运行的线程,您可以使用 Process Explorer

【讨论】:

    【解决方案2】:

    您可以使用Thread.getAllStackTraces()获取JVM中运行的所有线程及其堆栈跟踪

    【讨论】:

      【解决方案3】:

      回应您的以下评论

      在以下代码中:while(resultSet.next()) { name=resultSet.getString("hName"); MyRunnable 工人 = 新 MyRunnable(hName); threadExecutor.execute(worker); } 。我的主题 池的大小为 10。我需要确保我的程序正常工作 正确使用多线程并想检查有多少线程 在某个时刻运行。我该怎么做?

      对于另一个答案,我建议您使用JVisualVM 分析您的代码并检查您的线程池是否正常工作。这个建议背后的原因是,您不必为 JVM 管理的所有其他内务线程而烦恼。除了你想要做的是为什么像 JVisualVM 这样的工具是为之而生的。

      如果您是分析 Java 程序的新手,JVisualVM 可让您在运行代码时了解幕后情况。您可以查看堆、GC 活动,检查正在运行/等待任何样本/配置您的 CPU 或内存使用情况的线程。还有不少插件。

      【讨论】:

        【解决方案4】:

        从 Windows:

        肯定有一个performance counter 可以告诉你这个过程。

        以编程方式:

        Thread#activeCount:

        返回当前线程的线程组及其子组中活动线程的估计数。递归迭代当前线程的线程组中的所有子组。

        或者更直接,ThreadGroup#activeCount

        返回此线程组及其子组中活动线程数的估计值。

        ThreadGroup#getParent:

        返回此线程组的父级。

        首先,如果parent不为null,则调用父线程组的checkAccess方法,不带参数;这可能会导致安全异常。

        所有这些似乎都暗示了以下内容:

        int activeThreadTotalEstimate() {
            ThreadGroup group;
            ThreadGroup parent;
        
            group = Thread.currentThread().getThreadGroup();
            while ((parent = group.getParent()) != null) {
                group = parent;
            }
            return group.activeCount();
        }
        

        【讨论】:

          【解决方案5】:

          代码sn-p:

          import java.util.Set;
          import java.lang.Thread.State;
          public class ActiveThreads {
              public static void main(String args[]) throws Exception{
                  for ( int i=0; i< 5; i++){
                      Thread t = new Thread(new MyThread());
                      t.setName("MyThread:"+i);
                      t.start();
                  }
                  int threadCount = 0;
                  Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
                  for ( Thread t : threadSet){
                      if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup() && 
                          t.getState() == Thread.State.RUNNABLE){
                          System.out.println("Thread :"+t+":"+"state:"+t.getState());
                          ++threadCount;
                      }
                  }
                  System.out.println("Thread count started by Main thread:"+threadCount);
              }
          }
          
          class MyThread implements Runnable{
              public void run(){
                  try{
                      Thread.sleep(2000);
                  }catch(Exception err){
                      err.printStackTrace();
                  }
              }
          }
          

          输出:

          Thread :Thread[main,5,main]:state:RUNNABLE
          Thread count started by Main thread:1
          

          解释:

          Thread.getAllStackTraces().keySet() 为您提供了由Program and System 启动的所有线程的列表。在没有ThreadeGroup 条件的情况下,如果它们处于活动状态,您将获得系统线程计数。

          Reference Handler, Signal Dispatcher,Attach Listener and Finalizer

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-11
            相关资源
            最近更新 更多