Re: CompletableFuture#postComplete() can be invoked concurrently?
Martin Buchholz via Concurrency-interest <[email protected]> Sat, 22 Aug 2020 11:07:21 -0700
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CA+kOe08J5eEFx9Kgv83yfroSeR5zZ7y9N6xZcccptOMT2JyZZg@mail.gmail.com> |
This code is very tricky and hard to understand. When studying this, I would only look at latest version of the code. If you think you've found a bug, you should try to create a repro, preferably in jsr166 CVS. You can see some stress tests in the tests e.g look at expensiveTests in http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/tck/CompletableFutureTest.java?view=markup On Sat, Aug 22, 2020 at 8:19 AM Liu via Concurrency-interest <[email protected]> wrote: > > 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 _______________________________________________ Concurrency-interest mailing list [email protected] http://cs.oswego.edu/mailman/listinfo/concurrency-interest