Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the number of Threads/CPUs available to the Java VM?

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?)

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.

Thanks!

like image 751
Martin K Avatar asked Nov 15 '15 18:11

Martin K


People also ask

How many threads can a Java VM support?

Each JVM server can have a maximum of 256 threads to run Java applications.

Can JVM use multiple cores?

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.

How many threads should I use Java?

One thread per processor/core will maximize processing power and minimize context switching.

How do I get more threads on my CPU?

Select “Processor” and click “Properties.” A dialogue box should pop up and give you the option to turn hyper-threading on or off. Some manufacturers and providers may label the option as “Logical processor” or “Enable Hyper-threading.” The process will vary by manufacturer.


1 Answers

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

-XX:ActiveProcessorCount=2 
like image 63
mirekphd Avatar answered Sep 19 '22 15:09

mirekphd