title: Does the specification guarantee that operations on sequential Java streams have to stay in the current thread?
body: <p>Does the specification guarantee, that all operations on <strong>sequential</strong> Java Streams are executed in the current thread? (Except for &quot;forEach&quot; and &quot;forEachOrdered&quot;)</p>
  <p>I explicitly ask for the specification, not what the current implementation does. I can look into the current implementation myself and don't need to bother you with that. But the implementation might change and there are other implementations.</p>
  <p>I'm asking because of ThreadLocals: I use a Framework which uses ThreadLocals internally. Even a simple call like company.getName() eventually uses a ThreadLocal. I cannot change how that framework is designed. At least not within a sane amount of time.</p>
  <p>The specification seems confusing here. The documentation of <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="noreferrer">the Package &quot;java.util.stream&quot;</a> states:</p>
  <blockquote>
  <p>If the behavioral parameters do have side-effects, unless explicitly stated, there are no guarantees as to the visibility of those side-effects to other threads, <strong>nor are there any guarantees that different operations on the &quot;same&quot; element within the same stream pipeline are executed in the same thread</strong>.</p>
  <p>...</p>
  <p>Even when a pipeline is constrained to produce a result that is consistent with the encounter order of the stream source (for example, IntStream.range(0,5).parallel().map(x -&gt; x*2).toArray() must produce [0, 2, 4, 6, 8]), <strong>no guarantees are made</strong> as to the order in which the mapper function is applied to individual elements, or <strong>in what thread any behavioral parameter is executed</strong> for a given element.</p>
  </blockquote>
  <p>I would interpret that as: Every operation on a stream can happen in a different thread. But <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-" rel="noreferrer">the documentation of &quot;forEach&quot; and &quot;forEachOrdered&quot;</a> explicitly states:</p>
  <blockquote>
  <p>For any given element, the action may be performed at whatever time and in whatever thread the library chooses.</p>
  </blockquote>
  <p>That statement would be redundant if every stream operation could happen in an unspecified thread. Is therefore the opposite true: All operations on a serial stream are guaranteed to be executed in the current thread, except for &quot;forEach&quot; and &quot;forEachOrdered&quot;?</p>
  <p>I have googled for an authoritative answer about the combination of &quot;Java&quot;, &quot;Stream&quot; and &quot;ThreadLocal&quot; but found nothing. The closes thing was an answer by <a href="https://stackoverflow.com/users/3553087/brian-goetz">Brian Goetz</a> to a related question here on Stack Overflow, but it is about the order, not the thread, and it is only about &quot;forEach&quot;, not the other stream methods: <a href="https://stackoverflow.com/q/34247318/194860">Does Stream.forEach respect the encounter order of sequential streams?</a></p>