如何获取Java进程中的线程数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1922290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the number of threads in a Java process
提问by ndemir
How can I see the number of threads in a Java process?
如何查看 Java 进程中的线程数?
采纳答案by laura
Useful tool for debugging java programs, it gives the number of threads and other relevant info on them:
用于调试 Java 程序的有用工具,它提供线程数和其他相关信息:
jconsole <process-id>
jconsole <process-id>
回答by MarkPowell
There is a static method on the Thread
Class that will return the number of active threads controlled by the JVM:
类上有一个静态方法Thread
,它将返回由 JVM 控制的活动线程数:
Thread.activeCount()
Thread.activeCount()
Returns the number of active threads in the current thread's thread group.
返回当前线程的线程组中的活动线程数。
Additionally, external debuggers should list all active threads (and allow you to suspend any number of them) if you wish to monitor them in real-time.
此外,如果您希望实时监控它们,外部调试器应该列出所有活动线程(并允许您挂起任意数量的线程)。
回答by miku
java.lang.Thread.activeCount()
It will return the number of active threads in the current thread's thread group.
它将返回当前线程的线程组中的活动线程数。
docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()
文档:http: //docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()
回答by Upul Bandara
public class MainClass {
public static void main(String args[]) {
Thread t = Thread.currentThread();
t.setName("My Thread");
t.setPriority(1);
System.out.println("current thread: " + t);
int active = Thread.activeCount();
System.out.println("currently active threads: " + active);
Thread all[] = new Thread[active];
Thread.enumerate(all);
for (int i = 0; i < active; i++) {
System.out.println(i + ": " + all[i]);
}
}
}
回答by gustafc
ManagementFactory.getThreadMXBean().getThreadCount()
doesn't limit itself to thread groups as Thread.activeCount()
does.
ManagementFactory.getThreadMXBean().getThreadCount()
不像线程组那样将自己限制在线程组Thread.activeCount()
中。
回答by Ravindra babu
I have written a program to iterate all Threads
created and printing getState()
of each Thread
我编写了一个程序来迭代所有Threads
创建和打印getState()
的每个Thread
import java.util.Set;
public class ThreadStatus {
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()){
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();
}
}
}
Output:
输出:
java ThreadStatus
Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
Thread :Thread[main,5,main]:state:RUNNABLE
Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:4,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:3,5,main]:state:TIMED_WAITING
Thread count started by Main thread:6
If you remove below condition
如果您删除以下条件
if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup())
You will get below threads in output too, which have been started by system.
您也将在输出中获得以下线程,这些线程已由系统启动。
Reference Handler, Signal Dispatcher,Attach Listener and Finalizer
.
Reference Handler, Signal Dispatcher,Attach Listener and Finalizer
.
回答by Felipe Oliveira
Generic solution that doesn't require a GUI like jconsole (doesn't work on remote terminals), ps works for non-java processes, doesn't require a JVM installed.
不需要像 jconsole 这样的 GUI(不适用于远程终端)的通用解决方案,ps 适用于非 java 进程,不需要安装 JVM。
ps -o nlwp <pid>
ps -o nlwp <pid>
回答by Alihossein shahabi
Using Linux Top
command
使用 LinuxTop
命令
top -H -p (process id)
top -H -p (process id)
you could get process id of one program by this method :
您可以通过此方法获取一个程序的进程 ID:
ps aux | grep (your program name)
ps aux | grep (your program name)
for example :
例如 :
ps aux | grep user.py
ps aux | grep user.py