gpt4 book ai didi

java - Linux 中运行的线程数有最大限制吗?

转载 作者:太空宇宙 更新时间:2023-11-04 05:00:16 25 4
gpt4 key购买 nike

Linux 中运行的线程数有最大限制吗?是否可以计算java中所有类的线程总数?

最佳答案

从技术上讲,您应该使用 2 X numberOfPhysicalCores 线程以获得最佳性能。 This帖子描述了最大线程数(感谢@gavriel找到它:P)

接下来,获取当前JVM中正在运行的线程数:

public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(4);
service.execute(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
System.out.println(Thread.getAllStackTraces().size());

}

O/P : 5

关于java - Linux 中运行的线程数有最大限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35268493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com