This is really great. Could you also share an example of `allOf` with two independent tasks having different return types? Let's say getUserDtails returns a User object while the getWeatherDetails returns a Weather object. How do we combine these heterogeneous return types?
Thanks, this is very helpful. I have a simple use case where I want to fetch a value from cache. If the value is not available in cache, then I will return a fallback value from the main thread. However I want to start a thread in parallel to asynchronously call the underlying db so that cache is populated for future calls. I don't want this db call to block the main thread from returning. It's like a background task which will run while the main thread exits. Can runAsync support this? Or after the main thread exits, will runAsync process also finish?
Thank you! runAsync can support this. However by default runAsync will create daemon thread and hence program will not wait for it after main thread exists. You can avoid this by 1. Using join CompletableFuture completableFuture = CompletableFuture.runAsync(runnable); ... completableFuture.join(); OR 2. passing executorService while calling runAsync. ExecutorService executorService = Executors.newCachedThreadPool(); .. CompletableFuture.runAsync(runnable, executorService);
Nice explanation on this concept.. Now, I've a question - If i wanted to execute (call a service) three 3 different threads and handle exception for each one, how can i do it?
Thank you! for executing 3 different threads, you can take a look at example of "CompletableFuture.allOf". For exception handling only thing you need to do is add exceptionally part while creating CompletableFuture like below ==> return CompletableFuture.supplyAsync( () ->{ System.out.println( " future3 - "+ Thread.currentThread().getName()); delay(1); if(true) throw new RuntimeException(); return "3"; } ).exceptionally( e -> { System.out.println("Caught exception"); return "null"; });
It depends on use case. If data you are waiting for is mandatory to proceed you can raise an exception and quit application or show error on UI to user if it is UI application and ask them to retry.
Thanks Sir ! A good Job !
You are explaining nicely with good examples. Thanks
what a explanation sir... I seen so many but little bit confusion. when i saw your video it is very clear
Very clear and concise explanation!!, Thanks
Very nicely explained..thanks
Amazing explanation 👍
You are explaining nicely with good examples. Thanks
Fantastic tutorial, went smooth, very clear n explained very well. Many thanks!
Thank you!
Thanks for this video to understand ....
great sir.
nice way of explanation.. keep going bro
Thank you
Thank you for the information.
Very well explained..thanks a lot
Thank you! 🙂
Sir, your content is awesome!! Can you please make videos on different design patterns?
Thank you! Will keep design patterns in future list.
This is really great. Could you also share an example of `allOf` with two independent tasks having different return types? Let's say getUserDtails returns a User object while the getWeatherDetails returns a Weather object. How do we combine these heterogeneous return types?
Thank you! I will share this in subsequent video.
@@TechRecipesTR, thanks for the prompt reply. I'd wait for the video.
@@arvindjaiswal8013 Video is uploaded
Thanks, this is very helpful. I have a simple use case where I want to fetch a value from cache. If the value is not available in cache, then I will return a fallback value from the main thread. However I want to start a thread in parallel to asynchronously call the underlying db so that cache is populated for future calls.
I don't want this db call to block the main thread from returning. It's like a background task which will run while the main thread exits.
Can runAsync support this? Or after the main thread exits, will runAsync process also finish?
Thank you! runAsync can support this. However by default runAsync will create daemon thread and hence program will not wait for it after main thread exists. You can avoid this by
1. Using join
CompletableFuture completableFuture = CompletableFuture.runAsync(runnable);
...
completableFuture.join();
OR
2. passing executorService while calling runAsync.
ExecutorService executorService = Executors.newCachedThreadPool();
..
CompletableFuture.runAsync(runnable, executorService);
Great examples. For thenCombine() can you extend delay in first future and demo
Thanks for feedback. I will see if I can add one more video on this. Additionally you can also checkout code from link in description.
Video is uploaded answering commnet!
Nice explanation on this concept.. Now, I've a question - If i wanted to execute (call a service) three 3 different threads and handle exception for each one, how can i do it?
Thank you! for executing 3 different threads, you can take a look at example of "CompletableFuture.allOf". For exception handling only thing you need to do is add exceptionally part while creating CompletableFuture like below ==>
return CompletableFuture.supplyAsync(
() ->{
System.out.println( " future3 - "+ Thread.currentThread().getName());
delay(1);
if(true) throw new RuntimeException();
return "3";
}
).exceptionally( e -> {
System.out.println("Caught exception");
return "null";
});
please cover upload all methods
Thank you for comment. CompletableFuture.allOf() example can help you create upload all scenario. Please try let me know if you face isssue.
What is the best practice to handle TimeoutException of CompletableFuture.get(timeout, TIMEUNITS.SECOND)?
It depends on use case. If data you are waiting for is mandatory to proceed you can raise an exception and quit application or show error on UI to user if it is UI application and ask them to retry.