title: Java Multi thread slower than single thread
body: <p>I'm testing what happens when you run a code in multiple threads.
  Here's what I'm doing:</p>
  
  <p>1) Generate different matrix sizes from 50x50 to 1000x1000 with random values with a Java program I wrote called "Matrix Generator".</p>
  
  <p>2) Run another Java Program with N threads from 1 to 3 max (I have a quad core computer) where each thread calculate the Consistency of the matrix with Floyd Warshall algorythm. I don't parallelize the computation.</p>
  
  <p>The result I get is that as the matrix size increases, the execution time increases considerably when I use multithreading. It is faster when I use a single thread. See the statistics link below.</p>
  
  <p>Why is this happening? Is it because each thread starts its own code and the CPU bus causes a bottleneck because every thread wants to access the RAM?
  I used a CPU profiler and what I saw Is that as the matrix size increases, the CPU cache L2 and cache L3 misses increase considerably,</p>
  
  <p>Here's the Java code:</p>
  
  <pre><code>public class MultiThreadGraphs {
  public static void main(String[] args) {
      final int nFiles = 50;
      final int matrixSize;
      final int nThreads;
  
      Scanner reader = new Scanner(System.in);
  
      System.out.println("Enter matrix size: ");
      System.out.println("Available sizes: 50, 100, 150, 200, 300, 400, 500, 600, 1000");
      matrixSize = reader.nextInt();
  
      System.out.println("Enter n threads: ");
      nThreads = reader.nextInt();
  
      reader.close();
  
      System.out.println("Reading files..");
  
      List&lt;int[][]&gt; graphs = readFiles(matrixSize, nFiles);
  
      System.out.println("Total files: " + graphs.size());
  
      System.out.println("Running " + nThreads + " threads..");
  
      try {
          try {
              runThreads(graphs, nThreads);
          } catch (ExecutionException e) {
              e.printStackTrace();
          }
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  
  private static void runThreads(List&lt;int[][]&gt; graphs, int nThreads) throws InterruptedException, ExecutionException {
  
      ExecutorService executor = Executors.newFixedThreadPool(nThreads);
      Collection&lt;Callable&lt;Long&gt;&gt; callables = new ArrayList&lt;Callable&lt;Long&gt;&gt;();
  
      for(int i = 0; i &lt; nThreads; i++) {
          callables.add(new Callable&lt;Long&gt;() {
              @Override
              public Long call() throws Exception {
                  List&lt;int[][]&gt; callableGraphs = graphs;
  
                  Instant start = Instant.now();
  
                  for(int[][] graph: callableGraphs) {
                              FloydWarshall f = new FloydWarshall(graph);
                      f.checkConsistency();
                  }
  
                  Instant end = Instant.now();
  
                  Duration timeElapsed = Duration.between(start, end);
  
                  return timeElapsed.toNanos();
              }
          });
      }
  
      List&lt;Future&lt;Long&gt;&gt; futures = executor.invokeAll(callables);
  
      SummaryStatistics stats = new SummaryStatistics();
  
      for(Future&lt;Long&gt; future: futures) {
          stats.addValue(future.get());
      }
  
  
      String mean = String.valueOf(stats.getMean());
      double std = stats.getStandardDeviation();
  
      System.out.println("Execution time: " + (long)Double.parseDouble(mean));
  
      System.out.println("Mean: " + mean);
      System.out.println("Standard Deviation: " + std);
  
      executor.shutdown();
  }
  }
  </code></pre>
  
  <p>Statistics:
  <a href="https://docs.google.com/spreadsheets/d/1Wsgn14E9ltCWUHygC926xiz-vHEiENgSoHGzm_-BOa8/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1Wsgn14E9ltCWUHygC926xiz-vHEiENgSoHGzm_-BOa8/edit?usp=sharing</a></p>