gpt4 book ai didi

java - 应用程序在被 JVM 终止之前可以生成多少个线程

转载 作者:行者123 更新时间:2023-12-02 12:50:07 25 4
gpt4 key购买 nike

问:

1) 在多线程应用程序中,应用程序可以生成的线程数量是否存在限制。

2)如果存在这样的限制,JVM是否终止应用程序以及使用什么错误代码。

编辑

3)如果应用程序以非常快的速度连续生成,JVM 是否会将其检测为“粗糙”应用程序

提前致谢

最佳答案

您应该测试它的设置。我为我做的:

import static java.lang.Thread.currentThread;
import java.util.concurrent.CountDownLatch;

public class Test {
static final Thread t = currentThread();
public static void main(String[] args) throws Exception {
int i = 0;
try {
while (true) {
final CountDownLatch l = new CountDownLatch(1);
new Thread() { public void run() { l.countDown(); park(); }}.start();
l.await();
i++;
}
} finally { System.out.println("Started " + i + " threads."); }
}
private static void park() {
try { t.join(); } catch (InterruptedException e) {
System.out.println("Unparked");
}
}
}

输出:

Started 2030 threads.
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:658)
at test.Test.main(Test.java:10)

关于java - 应用程序在被 JVM 终止之前可以生成多少个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675141/

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