Java – directly from completable future Thenaccept return value

I tried to return a list from completable future as follows:

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

It failed "unexpected return type. But how to return the result asynchronously?

Solution

There is a fundamental contradiction between your goals You can only use get() to return a complete, directly usable list or "return results asynchronously"

If the method list < provider > get() should return a list, it can be used by callers without restrictions. It cannot maintain asynchronous operation, because the operation must have been completed when get() returns This can be as simple as calling join (), waiting for completion and getting the result:

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

Or

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

This effectively converts the potential asynchronous operation of getsomedata () into synchronous operation

This answer, use

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

It does not wait for the operation to complete, but returns a new ArrayList after scheduling the addall operation. The execution of the operation is not controlled by this method at all

This is an asynchronous operation, so when get () returns, the list may still be empty, but it will be updated by any thread later Because ArrayList is not thread safe, the perceived state does not have to be empty or complete. It can be any intermediate state or even consistent When using this code, be prepared for strange exceptions, seemingly impossible situations, or other surprises

When you fix this code by using a thread safe list implementation, you still encounter the following problems: the returned list may be empty at the time of query and filled at any point in time outside the caller control This is not repairable. As mentioned at the beginning, it is a basic problem that you want to operate asynchronously but return a list

Completabilefuture is already an encapsulation of potential asynchronous operations. Therefore, if you want the operation to remain asynchronous, just return completabilefuture < list < provider > > to the caller to gain control Otherwise, if you want the caller to receive a list that can be used indefinitely, wait for completion before returning the result, for example, through join()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>