title: Question about potential race condition with ParallelStream of Lists
body: <p>I came across this piece of code that uses Java streams, specifically parallelStream() in order to collect some data from an oracle database. See below where in this case:</p>
  
  <ul>
  <li>range = some list of input Id </li>
  <li>rangeLimit = 1000</li>
  <li>rangeLimitedFunction = some function that queries a DB for some content</li>
  </ul>
  
  <pre class="lang-java prettyprint-override"><code>ForkJoinPool threadPool = new ForkJoinPool(Math.min(Runtime.getRuntime().availableProcessors(), parallelism));
  
  try {
      Optional&lt;C&gt; res = threadPool.submit(new Callable&lt;Optional&lt;C&gt;&gt;() {
          @Override
          public Optional&lt;C&gt; call() throws Exception {
              return splitByLimit(range, rangeLimit).parallelStream()
                 .map(rangeLimitedFunction::apply)
                 .reduce((list, items) -&gt; {
                     list.addAll(items);
                     return list;
                 });
          }
      }).get();
  </code></pre>
  
  <p>From what I understand this is how this is working:</p>
  
  <ol>
  <li>Split range into chunks of 1000 to feed into the function</li>
  <li>Process each chunk in a thread to return some results</li>
  <li>Aggregate the results to a list of POJOs</li>
  </ol>
  
  <p>My question is around a potential race condition imposed by trying to reduce into a single list. Is it not possible for many of these threads to be trying to add content to the resulting list and potentially corrupt data?</p>