title: Performance implications of Java parallel streams
body: <p>What are the best practices around using <code>.stream().parallel()</code>? </p>
  
  <p>For example, if you have a bunch of blocking I/O calls and you want to check if <code>.anyMatch(...)</code>, doing this in parallel seems a sensible thing to do.</p>
  
  <p>Example code: </p>
  
  <pre><code>public boolean hasAnyRecentReference(JobId jobid) {
    &lt;...&gt;
    return pendingJobReferences.stream()
       .parallel()
       .anyMatch(pendingRef -&gt; { 
         JobReference readReference = pendingRef.sync();
         Duration referenceAge = timeService.timeSince(readReference.creationTime());
         return referenceAge.lessThan(maxReferenceAge)
       });
  }
  </code></pre>
  
  <p>At first glance this looks sensible because we can perform multiple blocking reads concurrently since we only care about any that match, instead of checking one after the other (so if every read takes 50ms we only have to wait for (<em>50ms</em> * <em>expectedNumberOfNonRecentRefs</em>) / <em>numThreads</em> ).</p>
  
  <p>Could introducing this code in a production environment have any unforeseen performance implications on other parts of the codebase?</p>