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?
#CompleteableFuture blocking before it is done?
22 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fiery edge! Please use
/closeor theClose Postbutton 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.
I also have a method that uses this thing on the main thread and will that block the thread?
Thanks!
<@&765578700724371486>
Requested by iHateSpigot#4458
CompletableFuture.get() method, which will block the current thread and wait for the completion of the CompletableFuture
i mean if this code is running on the main thread, it will block the main thread until the CompletableFuture is completed!
Then it is the same as just not using one :/
I thought it only blocks the method
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!
How can I make it only block the method? by putting it whole into that?
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!
Do you have an example for that?
completableFuture.thenAccept(result -> {
});```
whats wrong with u?
What?
I'm just wondering if I could, because all method I know is in lambda and I can't return value there.
completableFuture.thenAccept(this::processResult);
private void processResult(String result) {
}```
Oh, that helps! Thank you so much!
my pleasure!