How can I restrict my Java application to be run only on certain processors?
The Java Service Wrapper helps you configure your Java application so that it will always run on a designated subset of processors.
Normally it is left up to the operating system to decide which CPUs or cores of the machine will be used to run an application. Depending on the number of threads required by the application at any given time and the needs of other applications, the OS will try to move threads around as needed to keep the overall load on each CPU balanced. When there are more threads than CPUs (which is the norm), the OS will share CPUs amongst several threads, meaning that threads will be paused until it is their turn. For some applications, this method may not work well, and it can be useful to dedicate certain CPUs or groups of CPUs to specific applications. Most OS platforms provide the ability to specify that a process should only execute on specific CPUs. This is commonly called CPU affinity or CPU pinning.
Dedicating specific CPUs to your application can be a way to ensure that it will always have enough CPU resources. On the contrary, you could explicitly keep some processors unused in order to guarantee sufficient resources for other processes running on the same machine. Setting processor affinity can also significantly optimize CPU cache. In other words, whenever a process changes the CPU, its data is transferred from one CPU to another. This transfer is costly but you can minimize it by pinning the process to a limited number of CPUs. You should, however, be careful to allow enough processors for your application, as it may have the ability to execute parallel tasks on different CPUs when available.
Unfortunately, Java does not provide a solution to launch the JVM with core or processor affinity. Should you want to pin your Java application to specific CPUs, you would have to either use commands like procaff, taskset or invoke native code. These solutions are platform specific.