如何确定 Java-VM 中可用的最大线程数?
我想确定我能够为排序算法创建的最大线程数。我想为此使用 java.lang.Runtime 。
我想计算当前线程数量并在达到限制时停止创建新线程。
I want to determine the max number of threads I am able to create for my sort algoritm. I want to use java.lang.Runtime for that.
I want to count the current thread amount and stop creating new threads when the limit is reached.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JVM 的最大线程数通常为数千。如果您使用多个线程来优化计算算法,那么您实际上不需要的数量超过其运行的系统中的处理器数量。使用 Runtime.getRuntime().availableProcessors() 来查找。
The max number of threads for a JVM is generally somewhere in the thousands. If you're using multiple threads to optimize a computational algorithm you don't really want more than the number of processors in the system its run on. Use
Runtime.getRuntime().availableProcessors()
to find out.我建议你从稍微不同的角度来看待这个问题......
我的意思是,如果你问
并且你问这个,因为你认为你使用的线程越多越好 ,例如,您可以问
任务 x 在双核计算机上使用数千个线程非常不太可能执行得更好。例如,作为一般指南,对于 CPU 密集型任务(排序算法可能就是这样),最佳线程数请
参见 如何找到最佳线程数?
实际最大线程数取决于操作系统和 JVM 以及配置的内存量在 JVM 中使用。创建新线程需要一些操作系统内存,但不需要 Java 堆内存。这意味着,如果您创建数千个线程,则可能会耗尽内存,而您无法使用 -Xmx 来增加内存。事实上,您分配的 Java 堆内存越少,可用的本机内存就越多,因此您可以创建的线程就越多。
请参阅这篇文章和评论来了解如何您可以用笔和纸算出最大值。
话虽如此,如果您想在应用程序中使用无限数量的线程,您可以使用“Executors.newCachedThreadPool()”,它将创建一个线程池,创建所需数量的线程。我不建议在日常使用中使用这种类型的池,但这与您原来的问题有关。
希望有帮助。
I'd suggest you look at the problem from a slightly different angle...
what I mean is that if you're asking
and you're asking that because you think the most threads you use the better, you could instead ask
its very unlikely that task x will perform better with thousands of threads on a dual core machine for example. As a general guide for example, for CPU bound tasks (which a sort algorithm probably is), the optimal number of threads is
see How to find out the optimal amount of threads?
The actual maximum number of threads depends on the OS and JVM along with how much memory is configured for use within the JVM. Creating a new thread takes some OS memory but not Java' heap memory. This means that if you create thousands of threads, you might run out of memory which you can't bump up using -Xmx. In fact, the less Java heap memory you allocate, the more native memory is available and so the more threads you can create.
See this article and the comment to get a feel for how you can work out the max with pen and paper.
Having said all that, if you want to use an unlimited number of threads in your application, you can use the `Executors.newCachedThreadPool()' which will create a pool of threads, creating as many as are needed. I don't recommend using this type of pool for everyday usage, but its related to your original question.
Hope that helps.
您可以通过将 visualvm 附加到进程来查看有多少线程。我推荐 1.3.2 版本,并下载所有插件。
我还建议使用 Spring 及其执行器池。这是配置线程池大小的好方法。
You can see how many threads you have by attaching visualvm to your process. I recommend version 1.3.2 with all the plugins downloaded.
I'd also recommend using Spring and its Executor pools. It's a great way to be able to configure the size of the thread pool.