Is there a way to code Java application to use only 2 CPU cores of the CPU. For example I want to set a limit of the CPU utilization. Is this possible in Java?
I believe that's something that needs to be handled at the OS level, thus can't be controlled through java code. Take a look at this post. I hope it helps.
Is it possible to force an existing Java application to use no more than x cores?
If you use a pool of threads and limit the number of threads to 2, no more than 2 cores can be used at the same time. However this is a poor way of limiting CPU usage, and may not even be possible in your scenario (it depends on how many concurrent tasks your application must run).
You can't control this directly. But if you limit the number of threads used to two it will always use only up to two cores.
But note that
a) the actual cores used might vary since the scheduler might move threads from one core to the other
b) there might be other threads started by the JVM (e.g. for garbage collection), that you might not expect from just looking at your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With