简体   繁体   中英

Return value directly from CompletableFuture.thenAccept

I am trying to return a list from my CompletableFuture like this:

public List<Provider> get() {
    CompletableFuture<List<Provider>> providersResponse = getSomeData();
    return providersResponse.thenAccept((List<Provider> providers) -> {
        return providers;
    });
}

It fails with "unexpected return type though. How can I return the result in an async way?

There is a fundamental contradiction in your goal. You can have only either, get() returning a complete, directly usable list or “return the result in an async way”.

If the method List<Provider> get() is supposed to return a List which can be used by the caller without restrictions, it can't stay an asynchronous operation, as the operation must have been completed when get() returns. This can be achieved as simple as calling join() , to wait for the completion and get the result:

public List<Provider> get() {
    CompletableFuture<List<Provider>> providersResponse = getSomeData();
    return providersResponse.join();
}

or just

public List<Provider> get() {
    return getSomeData().join();
}

This effectively turns the potentially asynchronous operation of getSomeData() into a synchronous operation.

This answer , using

public List<Provider> get() {
    List<Provider> providers = new ArrayList<>();
    CompletableFuture<List<Provider>> providersResponse = getSomeData();
    providersResponse.thenAccept(providers::addAll);
    return providers;
}

does not wait for the completion of the operation, but returns a new ArrayList after scheduling an addAll operation, whose execution is entirely out of control of this method.

This is an asynchronous operation, so when get() returns, the List might still be empty, but get updated at a later time by an arbitrary thread. Since ArrayList is not thread safe, the perceived state doesn't have to be either, empty or completed, it may be an arbitrary in-between state, not even required to be consistent. Be prepared for strange exceptions, impossible-looking situations or other surprises when using that code.

When you fix that code by using a thread safe List implementation, you still have the problem that the returned List might be empty when queried and get filled at an arbitrary point of time outside of the caller's control. That's not fixable, as said at the beginning, it's the fundamental problem of wanting an asynchronous operation but returning a List .

The CompletableFuture is already an encapsulation of a potentially asynchronous operation, so if you want the operation to stay asynchronous, just return the CompletableFuture<List<Provider>> to the caller, to allow to gain control. Otherwise, if you want the caller to receive a List which can be used without restrictions, wait for the completion before returning the result, eg via join() .

Try this:

public List<Provider> get() {
    List<Provider> providers = Collections.synchronizedList(new ArrayList<>());
    CompletableFuture<List<Provider>> providersResponse = getSomeData();
    providersResponse.thenAccept(providers::addAll);
    return providers;
}

Try this:

public List<Provider> get() {
    return getSomeData().get();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM