CompletableFuture in Java 8 - (Part 2)

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 32

  • @Mohamed-uf5jh
    @Mohamed-uf5jh 3 місяці тому +1

    Thanks Sir ! A good Job !
    You are explaining nicely with good examples. Thanks

  • @Sreejasuresh-fy5vf
    @Sreejasuresh-fy5vf 6 місяців тому

    what a explanation sir... I seen so many but little bit confusion. when i saw your video it is very clear

  • @RahulNirmale-s7u
    @RahulNirmale-s7u 5 місяців тому

    Very clear and concise explanation!!, Thanks

  • @skhan3530
    @skhan3530 2 роки тому +1

    Very nicely explained..thanks

  • @shruthiharini3738
    @shruthiharini3738 2 роки тому +1

    Amazing explanation 👍

  • @rajuraj-nk1tk
    @rajuraj-nk1tk 3 роки тому +1

    You are explaining nicely with good examples. Thanks

  • @ChennaiCineCuts
    @ChennaiCineCuts 2 роки тому +2

    Fantastic tutorial, went smooth, very clear n explained very well. Many thanks!

  • @Vinoth.Padmanaban
    @Vinoth.Padmanaban 3 роки тому +1

    Thanks for this video to understand ....

  • @tousifiqbal1428
    @tousifiqbal1428 Рік тому +1

    great sir.

  • @anandpadamala451
    @anandpadamala451 Рік тому +1

    nice way of explanation.. keep going bro

  • @noureddinemeto
    @noureddinemeto 2 роки тому

    Thank you for the information.

  • @sptrivedi1989
    @sptrivedi1989 3 роки тому +1

    Very well explained..thanks a lot

  • @manognajoshik8465
    @manognajoshik8465 2 роки тому +1

    Sir, your content is awesome!! Can you please make videos on different design patterns?

    • @TechRecipesTR
      @TechRecipesTR  2 роки тому

      Thank you! Will keep design patterns in future list.

  • @arvindjaiswal8013
    @arvindjaiswal8013 2 роки тому +1

    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?

    • @TechRecipesTR
      @TechRecipesTR  2 роки тому +1

      Thank you! I will share this in subsequent video.

    • @arvindjaiswal8013
      @arvindjaiswal8013 2 роки тому

      @@TechRecipesTR, thanks for the prompt reply. I'd wait for the video.

    • @TechRecipesTR
      @TechRecipesTR  2 роки тому

      @@arvindjaiswal8013 Video is uploaded

  • @rohanbhatia5258
    @rohanbhatia5258 2 роки тому +1

    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?

    • @TechRecipesTR
      @TechRecipesTR  2 роки тому

      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);

  • @priyankasingireddy
    @priyankasingireddy 3 роки тому

    Great examples. For thenCombine() can you extend delay in first future and demo

    • @TechRecipesTR
      @TechRecipesTR  3 роки тому

      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.

    • @TechRecipesTR
      @TechRecipesTR  2 роки тому

      Video is uploaded answering commnet!

  • @rajeshg3570
    @rajeshg3570 3 роки тому +1

    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?

    • @TechRecipesTR
      @TechRecipesTR  3 роки тому +1

      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";
      });

  • @rambaburam6455
    @rambaburam6455 3 роки тому +1

    please cover upload all methods

    • @TechRecipesTR
      @TechRecipesTR  3 роки тому

      Thank you for comment. CompletableFuture.allOf() example can help you create upload all scenario. Please try let me know if you face isssue.

  • @LongNguyen-tw8ig
    @LongNguyen-tw8ig 2 роки тому

    What is the best practice to handle TimeoutException of CompletableFuture.get(timeout, TIMEUNITS.SECOND)?

    • @TechRecipesTR
      @TechRecipesTR  Рік тому

      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.