[concurrency-interest] Memory Semantics of CompletableFuture

thurstonn thurston at nomagicsoftware.com
Mon Oct 23 16:11:41 EDT 2017


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/


More information about the Concurrency-interest mailing list