Java memory model question
Paul Sandoz
paul.sandoz at oracle.com
Fri Mar 6 20:24:44 UTC 2020
> On Mar 5, 2020, at 6:09 PM, David Holmes <david.holmes at oracle.com> wrote:
>
>> Specifically I'm wondering: if a thread X launches a parallel stream that
>> writes at most once to each independent element of an array, can it be
>> assumed that when the stream processing ends, X will always read the value
>> of all written array elements? In other words, can the termination of the
>> stream be seen as a memory ordering barrier (in a weak sense)?
>
> I would have expected this to be explicitly stated somewhere in the streams documentation, but I don't see it. My expectation is that terminal operations would act as synchronization points.
>
Yeah, there is effectively an implicit “actions taken by each operation happen-before actions following the completion of the terminal operation in another thread”.
We are relying on the following with F/J usage:
* <p>Memory consistency effects: Actions in a thread prior to the
* submission of a {@code Runnable} or {@code Callable} task to an
* {@code ExecutorService}
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
* any actions taken by that task, which in turn <i>happen-before</i> the
* result is retrieved via {@code Future.get()}.
This is only called out more explicitly with respect to elements for ordered actions by forEachOrdered and the escape-hatch iterator.
We could ad something at the end of the Stream doc.
Paul.
More information about the jdk-dev
mailing list