[concurrency-interest] Memory Semantics of CompletableFuture

Martin Buchholz martinrb at google.com
Mon Oct 23 16:23:08 EDT 2017


Are you not expecting the lambdas to run concurrently?

On Mon, Oct 23, 2017 at 1:11 PM, thurstonn <thurston at nomagicsoftware.com>
wrote:

> Hello,
>
> I wanted to inquire about the memory visibility semantics/guarantees of
> CompletableFuture#runAfterBothAsync
> and
> CompetableFuture#thenRunAsync
>
> I have a dependency graph that looks like the following:
> 3 initial tasks (A, B, C) which run independently, then afterwards (looping
> through some rows)
> C depends on (prior) C and prior B
> B depends on (prior) B and prior A
> A depends on prior A,
>
> so code looks like (in a loop):
> C = C.runAfterBothAsync(B, lambda)
> B = B.runAfterBothAsync(A, lambda)
> A = A.thenRunAsync(lambda)
>
> the lambdas write to a shared array, but there is no clobbering (a single
> write to each cell), my understanding is that each "new" CF, is
> *guaranteed*
> to see the writes of any of its dependencies before it executes.  Surely
> that is correct, although CF's javadoc is strangely silent on any memory
> guarantees.
> Also I'm assuming the MV guarantees extend to the master thread (the one
> invoking the above code). i.e. any writes it has made happen before any
> reads/writes in the CF's
>
> Anyway, In my tests I get inconsistent results, surely due to some memory
> visibility issues; I'm not 100% sure it's not a problem on my end, but I
> wanted to get confirmation on what the memory visibility guarantees there
> are or are not;
>
>
> *I could instead, of course, join on all 3 CF's before "submitting" new
> ones, creating any new ones in each iteration, but I really like the above
> style and prefer not to have the master thread block at all (until all rows
> have been processed)
>
>
>
> --
> Sent from: http://jsr166-concurrency.10961.n7.nabble.com/
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20171023/68e0df27/attachment.html>


More information about the Concurrency-interest mailing list