I have the need to create an asynchronous, non-blocking task in Java 8, I would like to use CompletableFutures but I'm not sure if it fulfils my needs.
To simplify the case, let's say we have an API that retrieve some data for the user but at the same time want to start a separate task to do some operations. I don't need and don't want to wait for this separate task to finish, I want to send the response right away to the user. An example in mock code:
public Response doSomething(params) {
Object data = retrieveSomeData(params);
// I don't want to wait for this to finish, I don't care if it succeeds or not
doSomethingNoWait(data);
return new Response(data);
}
I was looking at CompletableFutures, something like this:
CompletableFuture.supplyAsync(this::doSomethingNoWait)
.thenApply(this::logSomeMessage);
What I would like to know is if that is the correct approach? Would the response be returned to the user before doSomethingNoWait finish what it has to do?
Thanks!