问题描述
问:
1) 在多线程应用程序中,一个应用程序可以产生多少线程是有限制的.
1) In a multi-threading application is there a limit to how many threads an application may spawn.
2)如果存在这样的限制,JVM 是否会终止应用程序以及出现什么错误代码.
2)If such a limit exists, does the JVM terminate the application and with what error code.
编辑
3)如果一个应用程序以非常快的速度生成,JVM会检测到它是一个粗糙"的应用程序
3)If an application spawns in very rapid sucsession, would the JVM detect that as a "rough" application
提前致谢
推荐答案
JVM 规范没有规定限制.操作系统可能(可能)像往常一样限制每个进程等.每个线程还将分配一个堆栈等,因此可能需要大量内存,因此取决于计算机首先达到的限制..
No limit specified by the JVM specification. The OS may (probably) limit it as usual per process etc though.Each thread will also allocate a stack etc so a lot of memory could be needed, so depending on the computer what limit is hit first..
请注意:不过,过多的线程通常效率低下.您的程序可以/应该以另一种方式更好地扩展.根据需要使用线程池(执行程序服务)、异步 I/O、fork/join 等.
Please note: Too many threads are usually inefficient though. Your program could/should probably scale better in another way. Using Thread pools (executor service), asynchronous I/O, fork/join etc depending on your needs.
另见:
这篇关于应用程序在被 JVM 终止之前可以生成多少线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!