title: Java Matrix Multiplication using Thread Pool
body: <p>I'm trying to implement a program that can do matrix multiplication using Callable (thread pool). I have this program below. But, I don't see any significant difference in execution time when I run this on one thread or 8 threads.</p>
  
  <p>I took 5 samples for one thread and 8 thread, they're as follows (all in milliseconds):</p>
  
  <p>1 thread - 5433.982472 , 6872.947063 , 6371.205237 , 6079.367443 , 5842.946494</p>
  
  <p>8 threads - 5260.792683 , 5517.047691 , 5314.208147 , 5739.747367 , 5585.621661</p>
  
  <p>I'm new to this, am I doing anything wrong?</p>
  
  <pre><code>package naivematmul;
  
  import java.util.concurrent.ExecutionException;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Future;
  import java.util.concurrent.Callable;
  
   class NaiveMatMul implements Callable&lt;Integer&gt;
  {
      private int n;
      private int a[][];
      private int b[][];
      private int sum;
      private int i;
      private int j;
  
  
      public NaiveMatMul(int n, int a[][], int b[][], int i , int j )
      {
              this.n = n;
              this.a = a;
              this.b = b;
              this.i = i;
              this.j = j;
              this.sum = sum;
      }
  
      public Integer call() throws Exception
      {
          for (int k = 0 ; k &lt; n ; k++)
           {
               sum = sum + a[i][k] * b[k][j];
  
           }
           return sum;
      }
  
      public static void main(String[] args) throws InterruptedException, ExecutionException 
      {
  
          int n;
          int[][] a, b, c;
  
          n = 512;
          a = new int[n][n];
          b = new int[n][n];
          c = new int[n][n];
  
           int threads = 8;
  
          ExecutorService executor = Executors.newFixedThreadPool(threads);
  
          for(int i=0; i&lt;n; i++)
          {
              for(int j=0; j&lt;n; j++)
              {
                  a[i][j] = 1;
              }
          }
  
          for(int i=0; i&lt;n; i++)
          {
              for(int j=0; j&lt;n; j++)
              {
  
                  b[i][j] = 1;
  
              }
          }
  
  
            int sum = 0;
           long start_time = System.nanoTime();
        Future&lt;Integer&gt; future;
  
          for (int i = 0; i &lt; n ; i++)
          {
              for (int j = 0 ; j &lt; n ; j++)
              {
                  future = executor.submit(new NaiveMatMul(n, a, b, i, j));
                  c[i][j] = future.get();
                  sum = 0;
              }
  
          }
  
  
          long end_time = System.nanoTime();
          double difference = (end_time - start_time)/1e6;
          executor.shutdown();
  
  
  
  
                  System.out.println("Time taken : " + difference);
  
  
  
      }
  }
  </code></pre>