Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Multithread Application uses only one Core

I have a problem with my JVM Running on a CentOS 6.0 with openJDK 1.7.0_51 64Bit. My System is a 4-Core System with 8GB Ram.

I'm running a Java multithread application that I wrote myself. It's supposed to insert tons of Data into a NoSQL Database. For that, I'm spawning 4 threads, using a "CachedThreadPoolExecutor" from java.concurrent.Executors.

I instantiate 4 Workers that implement the "Runnable" Interface. Afterwards I execute the Thread using the threadpool. Here's 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 running this, using a parameter of 4, it will spawn 4 Threads in the Threadpool. I assumed that the JVM or my OS would be smart enough to schedule these threads on all of my cores. HOWEVER, only one core of my cpu is working at 100%,the others remain almost idle.

Am I doing anything wrong in my code or is this a JVM/OS problem. If so, is there anything I can do about that? Running this application on only 1 core is extremeley slowing down the whole app.

Help is greatly appreciated :)

like image 633
hoffmax91 Avatar asked Jan 22 '14 17:01

hoffmax91


1 Answers

Please bear in mind that its the OS and not the JVM responsible for CPU affinity - which is why I suggested that you first figure out how many CPU's you have and then perhaps use schedutils to configure processor affinity for a certain process.

cpu info - use one of the three below

/proc/cpuinfo
lscpu
nproc

install schedutils to confgure processor affinity

yum install schedutils  

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

taskset -c 2 -p 23564
like image 99
ali haider Avatar answered Oct 01 '22 21:10

ali haider