CompletableFuture#postComplete() can be invoked concurrently?
Liu via Concurrency-interest <[email protected]> Sat, 22 Aug 2020 23:16:52 +0800 (GMT+08:00)
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <[email protected]> |
In CompletableFuture, when I invoke the thenApply() on a CompletableFuture Object,
There is a subtle point in time, and it will make CompletableFuture#postComplete()
invoked concurrently.
private <V> CompletableFuture<V> uniApplyStage(
Executor e, Function<? super T,? extends V> f) {
if (f == null) throw new NullPointerException();
CompletableFuture<V> d = new CompletableFuture<V>();
if (e != null || !d.uniApply(this, f, null)) {
UniApply<T,V> c = new UniApply<T,V>(e, d, this, f);
push(c);
// at this point, this CompletableFuture Object just invoke the completeThrowable(),
// because the process to execute Supplier throw a exception.
c.tryFire(SYNC);
}
return d;
}
The thread that invoke c.tryFire(SYNC) and the thread that iovoke completeThrowable() will invoke
c.tryFire(NESTED) soonly in postComplete() . The important thing is the two "c" is same Object.
So the two threads can invoke c.tryFire() concurrently, and in some special
timing, the (d = dep) == null check can't work out.
1. The thread that invoke c.tryFire(SYNC) will invoke a.postComplete() in postFire() directly.
2. The thread that iovoke completeThrowable() will not invoke a.postComplete() in postFire(),
but it will return to the postComplete().
Above two sentence, the two threads will invoke postComplete() on same CompletableFuture object.
I'm afraid I didn't express it clearly. If you don't understand my opinion, just tell me if
CompletableFuture#postComplete() is designed to invoked concurrently or not?
PS: above code from JDK8, and I think this problem will happen too in newest JDK, if I am right about my opinion.
--------------------------------------------------------------------------------
Regards
Liu
_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest