#CompleteableFuture blocking before it is done?

22 messages · Page 1 of 1 (latest)

fiery edge
#

I have code CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> plugin.getRedisManager().getPool().getResource().get(key)); return completableFuture.get(); that gets things from Redis and return as a String and the method is running on main thread. I'm wondering if while is it doing is it blocking the main thread or just the method?

cosmic mossBOT
#

This post has been reserved for your question.

Hey @fiery edge! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fiery edge
#

I also have a method that uses this thing on the main thread and will that block the thread?

#

Thanks!

cosmic mossBOT
#

<@&765578700724371486>

Requested by iHateSpigot#4458
viscid sundial
fiery edge
#

Then it is the same as just not using one :/

fiery edge
viscid sundial
#

yesss! the get() method of a CompletableFuture will block the current thread and wait for the completion of the CompletableFuture, regardless of whether it is called from a method or directly from the main thread!

fiery edge
viscid sundial
#

use the CompletableFuture API to specify a callback function or a separate thread to handle the result of the async operation, instead of using the get() method to block the current thread!

fiery edge
#

Do you have an example for that?

viscid sundial
#
completableFuture.thenAccept(result -> {
    
});```
fiery edge
#

Is there a way to not do it in lambda

#

😦

viscid sundial
#

whats wrong with u?

fiery edge
#

What?

#

I'm just wondering if I could, because all method I know is in lambda and I can't return value there.

viscid sundial
#
completableFuture.thenAccept(this::processResult);

private void processResult(String result) {

}```
fiery edge
#

Oh, that helps! Thank you so much!

viscid sundial
#

my pleasure!