Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use only 2 CPU cores with Java [duplicate]

Tags:

java

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?

like image 210
Peter Penzov Avatar asked Dec 04 '13 14:12

Peter Penzov


3 Answers

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?

like image 83
jarz Avatar answered Sep 22 '22 14:09

jarz


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

like image 28
Raffaele Avatar answered Sep 19 '22 14:09

Raffaele


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.

like image 30
Jens Schauder Avatar answered Sep 22 '22 14:09

Jens Schauder