我可以设置Java VM可用的线程/CPU数量吗? [英] Can I set the number of Threads/CPUs available to the Java VM?

查看:76
本文介绍了我可以设置Java VM可用的线程/CPU数量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制Java VM可用的线程/进程的数量,类似于设置可用内存的方式.我希望能够将其指定为仅使用1个线程或任意数字.

I would like to limit the number of threads/processes available to the Java VM, similar to how you set the available memory. I would like to be able to specify it to just use 1 thread, or an arbitrary number.

注意:我无法在代码中进行设置,因为我想限制的代码是无法修改源代码的库.因此,它必须是对虚拟机级别的硬性限制.(或者,如果您可以对应用程序本身施加线程限制以覆盖库?)

NOTE: I cannot set it in the code, as the code that I would like to limit is a library where I cannot modify the source. So it must be a hard cap imposed on the level of the virtual machine. (Or if you could impose a thread limit on the application itself that could override libraries?)

注2:这样做的目的是进行性能测试,以限制我要测试的库,以查看它在访问不同数量的CPU/线程时的性能.

NOTE2: The purpose of this is a performance test, to throttle a library I want to test, to see how well it would perform when it has access to a different number CPUs/Threads.

谢谢!

推荐答案

JVM中的CPU限制问题已在Java 10中解决,并从内部版本8u191移植到Java 8:

The problem of CPU limits in JVM was solved in Java 10 and is backported to Java 8 from build 8u191:

-XX:ActiveProcessorCount=2

这篇关于我可以设置Java VM可用的线程/CPU数量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆