title: Multi threaded matrix multiplication performance issue
body: <p>I am using java for multi threaded multiplication. I am practicing multi threaded  programming. Following is the code that I took from another post of stackoverflow.</p>
  
  <pre><code>public class MatMulConcur {
  
  private final static int NUM_OF_THREAD =1 ;
  private static Mat matC;
  
  public static Mat matmul(Mat matA, Mat matB) {
  matC = new Mat(matA.getNRows(),matB.getNColumns());
  return mul(matA,matB);
  }
  
  private static Mat mul(Mat matA,Mat matB) {
  
  int numRowForThread;
  int numRowA = matA.getNRows();
  int startRow = 0;
  
  Worker[] myWorker = new Worker[NUM_OF_THREAD];
  
  for (int j = 0; j &lt; NUM_OF_THREAD; j++) {
      if (j&lt;NUM_OF_THREAD-1){
          numRowForThread = (numRowA / NUM_OF_THREAD);
      } else {
          numRowForThread = (numRowA / NUM_OF_THREAD) + (numRowA % NUM_OF_THREAD);
      }
      myWorker[j] = new Worker(startRow, startRow+numRowForThread,matA,matB);
      myWorker[j].start();
      startRow += numRowForThread;
  }
  
  for (Worker worker : myWorker) {
      try {
          worker.join();
      } catch (InterruptedException e) {
  
      }
    }
    return matC;
   }
  
  private static class Worker extends Thread {
  
  private int startRow, stopRow;
  private Mat matA, matB;
  
  public Worker(int startRow, int stopRow, Mat matA, Mat matB) {
      super();
      this.startRow = startRow;
      this.stopRow = stopRow;
      this.matA = matA;
      this.matB = matB;
  }
  
  @Override
  public void run() {
      for (int i = startRow; i &lt; stopRow; i++) {
          for (int j = 0; j &lt; matB.getNColumns(); j++) {
              double sum = 0;
              for (int k = 0; k &lt; matA.getNColumns(); k++) {
                  sum += matA.get(i, k) * matB.get(k, j);
              }
              matC.set(i, j, sum);
          }
      }
    }
  }
  </code></pre>
  
  <p>I ran this program for 1,10,20,...,100 threads but performance is decreasing instead. Following is the time table</p>
  
  <ol>
  <li>Thread 1 takes 18 Milliseconds</li>
  <li>Thread 10 takes 18 Milliseconds</li>
  <li>Thread 20 takes 35 Milliseconds </li>
  <li>Thread 30 takes 38 Milliseconds</li>
  <li>Thread 40 takes 43 Milliseconds </li>
  <li>Thread 50 takes 48 Milliseconds</li>
  <li>Thread 60 takes 57 Milliseconds</li>
  <li>Thread 70 takes 66 Milliseconds</li>
  <li>Thread 80 takes 74 Milliseconds </li>
  <li>Thread 90 takes 87 Milliseconds</li>
  <li>Thread 100 takes 98 Milliseconds</li>
  </ol>
  
  <p>Any Idea? </p>