title: Timing and multithreading in Java
body: <p>How does multithreading affect timing of methods? I have a method that looks like: </p>
  
  <pre><code>        NeighborThread[] threads = new NeighborThread[4];
          for (int i = 0; i &lt; vertixCores.length - 1; i++) {
  
              threads[i] = new NeighborThread(...);
              threads[i].start();
          }
  
          for (Thread thread : threads) {
              try {
                  thread.join();
              } catch (InterruptedException e) {
              }
          }
  </code></pre>
  
  <p>I am timing an algorithm, of which this method is a part of. My timing looks like: </p>
  
  <pre><code>        for (int i = 1; i &lt;= 18; i++) {
  
              final int  n          = 1 &lt;&lt; i;
              long total_time = 0;
  
              for (int j = 1; j &lt;= 5; j++) {
                  final long t0 =  System.nanoTime();
                  runAlgorithm();
                  long endTime = System.currentTimeMillis();
                  total_time += (System.nanoTime() - t0);
              }
              System.out.println("N = " + n + ": " + ((double) total_time / 1000 / 1000 / 1000 / 5) + " s");
          }
  </code></pre>
  
  <p>Will the timing be accurate? I thought it would be since the algorithm shouldn't proceed until all generated threads have completed.</p>