I am trying to understand the CompletableFuture interface in Java 8. My current code is:
CompletableFuture.supplyAsync(() -> wachtwoordBijwerken(gebruikersnaam, wachtwoord))
.thenAccept(this::saveOrUpdateGebruiker)
.exceptionally(e ->
{
log.error("Fout bij het bijwerken van wachtwoord voor gebruiker: " + gebruikersnaam, e);
return null;
});
I expected the call saveOrUpdateGebruiker()
to run in the main thread after the async call in the newly created thread is completed.
However, the call is still in another thread, which causes problems in the underlying hibernate implementation.
Is there a way to use CompletableFuture
for a non blocking async call, and still being able to use the result in my current thread?