How do I make Java use all the CPU power on the machine?

I sometimes write code in Java and I noticed that sometimes it uses more than 100% of the CPU on a multi-core machine. Now I am running some code on a multicore machine that has 33 processors (Amazon EC2) and I want my Java process to use all available processors so that it will have a very high machine utilization rate. Is this possible, or is it left up to Java to decide when to use more than 100% of the CPU? I don't want to change my code to use multithreading.

+3


source to share


2 answers


Without using multiple threads or processes (or something other than one process), you cannot achieve n * 100% utilization on an n-core computer.



The operating system decides to run each of your user programs on the same thread, unless more requests are requested. Without this request, the OS will happily let you run your code on a single thread, potentially maximizing entire CPU cycles, but that leaves the rest open.

+6


source


Unless your program is clearly multithreaded (you don't start a thread yourself), the only reason it might use more than one core is because you are using some of the functionality of a library that ultimately delegates work to the thread pool. Apparently you don't have much control over this, so you can't make it work anymore.



Regarding the use of all processors in your S3 instance: you can run your program multiple times (for example 33 times if you have 33 processors). If this is not possible, you will need to figure out how much of your processing is creating work in the thread pool and will change your program to do more (in parallel).

+2


source







All Articles