title: Calling java.util.Collections.shuffle() from multiple threads?
body: <p>I'm doing a school assignment where we're supposed to "translate" a C++ program to Java and compare the performance. The program uses different execution methods (newSingleThreadExecutor and newFixedThreadPool) to run some work sequentially and with different sized thread pools. The work is local to each thread - creating an ArrayList of random numbers, writing some random numbers to temporary files, and shuffling the ArrayList.</p>
  
  <p>I've run into a problem where the sequential execution is faster than the concurrent execution, and I seem to have pinpointed it down to using <code>java.util.Collections.shuffle(myArrayListOfIntegers)</code>. This seems to use a static instance of <code>java.util.Random</code> to shuffle the list, and that seems to be what's causing my problems - if I make my own shuffle method, exactly like the above mentioned one but using a new instance of Random for every call, things improve greatly.</p>
  
  <p><strong>Question 1.</strong> Am I correct in assuming that the static Random instance is what's causing my problems?</p>
  
  <p>The <code>java.util.Collections.shuffle(List&lt;?&gt; list)</code> method looks like this</p>
  
  <pre><code>public static void shuffle(List&lt;?&gt; list) {
      Random rnd = r;
      if (rnd == null)
          r = rnd = new Random(); // harmless race.
      shuffle(list, rnd);
  }
  
  private static Random r;
  </code></pre>
  
  <p><strong>Question 2.</strong> What is the <code>Random rnd = r;</code> in the above method supposed to do? Doesn't it just copy the reference? Why not use <code>Random rnd = new Random()</code>instead (which seems to solve my problems)?</p>