Java 中的线程数有硬性限制吗?
一些 来源说,你对 Java 中的线程数量有一个硬性限制(比如 15k 或 30k),甚至如果您没有操作系统上限且 RAM 不受限制。我还听说,在 Java 7 中这个限制被取消了。这两种说法都属实吗?
Some sources say, that you have a hard limit to a number of threads in Java (like 15k or 30k) even if you have no OS cap per that and unlimited RAM. I also heard, that in Java 7 this limit is lifted. Are both statements true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 虚拟机规范 没有指定限制关于线程的数量。它们通常受到可用堆栈空间量的限制,因为每个线程都有自己的私有堆栈。 (在尝试创建新线程时,无法分配堆栈通常会触发 OutOfMemory 异常。)我相信线程池和其他机制也可用于限制线程数量。 (例如,Sun 使用此方法Java System Portal Server 来限制事务数量。)
不同的虚拟机实现可能会施加其他限制。例如,BlackBerry 操作系统将每个应用程序的非系统线程限制为 16 个,总数为 64 个。我预计其他虚拟机制造商也施加了额外的限制。我会向虚拟机制造商咨询这个问题的答案。
The Java Virtual Machine Specification doesn't specify a limit on the number of threads. They are typically limited by the amount of stack space available, since each thread gets its own private stack. (The inability to allocate a stack is what usually triggers an OutOfMemory exception when trying to create a new Thread.) I believe that thread pools and other mechanisms can be used to also limit the number of threads. (This is used, for example, by the Sun Java System Portal Server to throttle the number of transactions.)
Different virtual machine implementations may impose other constraints. For instance, the BlackBerry OS restricts non-system threads to 16 per application and 64 total. I expect that other VM makers have also imposed additional constraints. I'd check with the VM manufacturer for an answer to this.