[concurrency-interest] How to subclass CompletableFuture?

Doug Lea dl at cs.oswego.edu
Thu Oct 30 19:58:45 EDT 2014


On 10/30/2014 05:46 PM, cowwoc wrote:

> Hi Doug,
>
> Holger pretty much nailed the answer, but I did have a related question:
> is complete() equivalent to internalComplete()?

For your purposes, yes. The internals shouldn't matter in
any usages, but complete() atomically establishes result
using internalComplete (and its variants), and then non-atomically
triggers dependents using postComplete.

> Meaning, can one
> implement supplyAsync() as follows?
>
> CompletionStage<T> supplyAsync(Supplier<T> s, Executor e)
> {
>     CompletionStage<T> result = new MyCompletionStage(...);
>     e.execute(() ->
>     {
>       result.complete(s.get());
>     });
>     return result;
> }

Yes.

> Imagine we have
> MyCompletionStage.supplyAsync(...).thenApplyAsync(...).thenComplete(...);

There is no "thenComplete", so I'm not sure what you are asking?

>
> The Javadoc for complete() sound as if we're locking the future's value
> such that subsequent operations (i.e. thenApplyAsync()) cannot invoke
> complete() with a different value.

In general yes. (There is a last-resort escape hatch
obtrudeValue, with known use cases only for failure recovery.)

>
> Is there a way to clarify the Javadoc of CompletableFuture or
> CompletionStage on this point (ideally by way of example code)?
>

Please let us know what kinds of wordings and examples would be
helpful!

-Doug




More information about the Concurrency-interest mailing list