title: Why can't stream of streams be reduced un parallel ? / stream has already been operated upon or closed
body: <h3>Context</h3>
  
  <p>I've stumble upon a rather annoying problem : I've a program with a lot of data source that are able to stream the same type of elements and I want to "map" each availiable element in the program (element order doesn't matter).</p>
  
  <p>Therefore I've tried to reduce my <code>Stream&lt;Stream&lt;T&gt;&gt; streamOfStreamOfT;</code> into a simple <code>Stream&lt;T&gt; streamOfT;</code> using <code>streamOfT = streamOfStreamOfT.reduce(Stream.empty(), Stream::concat);</code></p>
  
  <p>Since element order is not important for me, I've tried to parallelize the reduce operation with a <code>.parallel()</code> : <code>streamOfT = streamOfStreamOfT.parallel().reduce(Stream.empty(), Stream::concat);</code> But this triggers an <code>java.lang.IllegalStateException: stream has already been operated upon or closed</code></p>
  
  <h3>Example</h3>
  
  <p>To experience it yourself just play with the following main (java 1.8u20) by commenting / uncommenting the <code>.parallel()</code></p>
  
  <pre><code>public static void main(String[] args) {
      // GIVEN
      List&lt;Stream&lt;Integer&gt;&gt; listOfStreamOfInts = new ArrayList&lt;&gt;();
      for (int j = 0; j &lt; 10; j++) {
          IntStream intStreamOf10Ints = IntStream.iterate(0, i -&gt; i + 1)
                  .limit(10);
          Stream&lt;Integer&gt; genericStreamOf10Ints = StreamSupport.stream(
                  intStreamOf10Ints.spliterator(), true);
          listOfStreamOfInts.add(genericStreamOf10Ints);
      }
      Stream&lt;Stream&lt;Integer&gt;&gt; streamOfStreamOfInts = listOfStreamOfInts
              .stream();
      // WHEN
      Stream&lt;Integer&gt; streamOfInts = streamOfStreamOfInts
              // ////////////////
              // PROBLEM
              //    |
              //    V
              .parallel()
              .reduce(Stream.empty(), Stream::concat);
  
      // THEN
      System.out.println(streamOfInts.map(String::valueOf).collect(
              joining(", ")));
  }
  </code></pre>
  
  <h3>Question</h3>
  
  <p>Can someone explain this limitation  ? <strike>/ find a better way of handling parallel reduction of stream of streams</strike></p>
  
  <hr>
  
  <h3>Edit 1</h3>
  
  <p>Following @Smutje and @LouisWasserman comments it seems that <code>.flatMap(Function.identity())</code> is a better option that tolerates <code>.parallel()</code> streams</p>