title: Poor Multi-threading performance compared to Multi-processing in Java
body: <p>Assume that we have several million long lines of text that must be parsed.<br>
  On my i7 2600 CPU it takes about 13 milliseconds to parse every 1000 lines.<br>
  Therefore, parsing 1,000,000 lines takes around 13 seconds.<br>
  To decrease execution time, I have managed using multiple threads.<br>
  Using a blocking queue, I push 1,000,000 lines as a set of 1000 chunk each containing 1000 lines and consume the chunks using 8 threads. The code is simple and seems to be working however, the performance is not encouraging and takes around 11 seconds.<br>
  Here is the main fraction of multi-threaded code:</p>
  
  <pre><code>    for(int i=0;i&lt;threadCount;i++)
      {
          Runnable r=new Runnable() {
              public void run() {
                  try{
                      while (true){
                          InputType chunk=inputQ.poll(10, TimeUnit.MILLISECONDS);
                          if(chunk==null){
                              if(inputRemains.get())
                                  continue;
                              else
                                  return;
                          }
                          processItem(chunk);
                      }
                  }catch (Exception e) {
                      e.printStackTrace();  
                  }
              }
          };
          Thread t=new Thread(r);
          threadList.add(t);
          for(Thread t: threads)
              t.join();
  </code></pre>
  
  <p>I have used ExecutorService too but the performance is worse!<br>
  Changing the chunk size does not help too and the perfomance does not improve.<br>
  It means that the blocking queue is not a bottleneck.<br>
  On the other hand, when I run 4 instances of the serial program concurrently, it just takes 15 seconds to all 4 instances finish. This means that I can process 4,000,0000 lines using 4 process in 15 seconds and hence, the speed up is around 3.4 that is very promising compared to 1.2 speed up of multi-threading.<br></p>
  
  <p>I am wondering that anyone has any idea about this?<br>
  The problem is very straight forward: a set of lines in a blocking queue and several threads that pol items from the queue and process them in parallel. The queue is filled initially so the threads are fully busy.<br>
  I had similar experiences before too but I can not figure out why multi-processing is better.<br>
  I should also mention that I run the test on Windows 7 and using a 1.7 JRE.<br> 
  Any idea is welcomed and thanks before hand.</p>