Java Multithread Application uses only one Core

I have a problem with my JVM Running on CentOS 6.0 with openJDK 1.7.0_51 64Bit. My system is a 4-core system with 8 GB of frame.

I am running a multi-threaded Java application that I wrote myself. It should insert tons of data into a NoSQL database. To do this, I create 4 threads using the "CachedThreadPoolExecutor" from java.concurrent.Executors.

I am creating an instance of 4 workers that implement the "Runnable" interface. After that, I start Thread using threadpool. Here is my code:

public void startDataPump(int numberOfWorkers){ //class "DataPump" implements runnable for (int i = 0; i < numberOfWorkers; i++){ DataPump pump = new DataPump(); //"workerList" contains all workers and is a simple arrayList to keep track of the workers workerList.add(pump); //"workers" is the thradpool that has been //initialized earlier with "Executors.newCachedThreadPool() workers.execute(pump); } } 

When starting this mode using parameter 4, it will spawn 4 threads in Threadpool. I assumed that the JVM or my OS would be smart enough to schedule these threads in all of my cores. HOWEVER, only one core of my processor is working at 100%, the rest remain almost inactive.

Am I doing something wrong in my code or is it a JVM / OS problem. If so, can I do something about this? Running this application on only 1 core extremely slows down the entire application.

Help with thanks :)

+6
source share
3 answers

Please keep in mind that its OS, and not the JVM responsible for affixing the processor, is why I suggested first finding out how much processor you have and then possibly using schedutils to configure the processor affinity for a particular process.

cpu info - use one of the three below

 /proc/cpuinfo lscpu nproc 

install schedutils to compromise the processor

 yum install schedutils 

You can assign cpu affinity via schedutils as follows (2 is the second proceccor and 23564 is the process identifier):

 taskset -c 2 -p 23564 
+4
source

Scheduling a thread is not a JVM activity, but it is an OS activity.if, since the OS detects that the threads are independent of each other and can be executed separately, then it schedules it on a different core.

I'm not sure about the layouts, but I think it works at the application level (it allows you to set an affinity for the processor, but the last decision is made by the OS)

one thing about using kernels - the OS scheduler plans new processes on new kernels, since each process has its own process area independent of other processes (thus, they can be executed in parallel without any obstacles)

Try to create a new process for each thread that will help improve the use of your processor (using more cores), but there are also disadvantages. Each process creates its own process area, so each process requires additional memory (for each thread in your case), if you have a good amount of available memory, you can try this.

if it is just a Linux system, then the sar command is sufficient to monitor the use of the processor core (sar is the basic package in Linux, almost all utilities use sar, so the system overhead will be less).

+1
source

If your environment is virtual or, on the other hand, uses special CPU planning, such as docker, it is not possible to automatically configure Java to detect the many available cores and use all of them. You must indicate how many kernels you want to use through

In JDK> = 10, use the following JDK parameters:

-XX: ActiveProcessorCount = 2

In JDK> = 8, use the following JDK parameters:

-XX: + UnlockExperimentalVMOptions> -XX: ActiveProcessorCount = 2

0
source

All Articles