From abb1f462b9d3ddddab54eba7fc6c1684f26e6b21 Mon Sep 17 00:00:00 2001 From: Wynn Teo <49014791+wynnteo@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:11:25 +0800 Subject: [PATCH] Bael 7541 (#15877) * BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * BAEL-7541 compare runAsync and supplyAsync --- .../runvssupply/RunAndSupplyCompare.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-2/src/main/java/com/baeldung/runvssupply/RunAndSupplyCompare.java diff --git a/core-java-modules/core-java-concurrency-2/src/main/java/com/baeldung/runvssupply/RunAndSupplyCompare.java b/core-java-modules/core-java-concurrency-2/src/main/java/com/baeldung/runvssupply/RunAndSupplyCompare.java new file mode 100644 index 0000000000..a3b80d2db2 --- /dev/null +++ b/core-java-modules/core-java-concurrency-2/src/main/java/com/baeldung/runvssupply/RunAndSupplyCompare.java @@ -0,0 +1,79 @@ +package com.baeldung.runvssupply; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class RunAndSupplyCompare { + + public static void main(String args[]) throws ExecutionException, InterruptedException { + chainingOperationCompare(); + } + + public static void inputAndReturnCompare() throws ExecutionException, InterruptedException { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + // Perform non-result producing task + System.out.println("Task executed asynchronously"); + }); + + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + // Perform result-producing task + return "Result of the asynchronous computation"; + }); + // Get the result later + String result = supplyAsyncFuture.get(); + System.out.println("Result: " + result); + } + + public static void exceptionHandlingCompare() { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + // Task that may throw an exception + throw new RuntimeException("Exception occurred in asynchronous task"); + }); + try { + runAsyncFuture.get(); + // Exception will be thrown here + } catch (ExecutionException ex) { + Throwable cause = ex.getCause(); + System.out.println("Exception caught: " + cause.getMessage()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + // Task that may throw an exception + throw new RuntimeException("Exception occurred in asynchronous task"); + }) + .exceptionally(ex -> { + // Exception handling logic + return "Default value"; + }); + + Object result = supplyAsyncFuture.join(); + // Get the result or default value + System.out.println("Result: " + result); + } + + public static void chainingOperationCompare() { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + // Perform non-result producing task + System.out.println("Task executed asynchronously"); + }); + runAsyncFuture.thenRun(() -> { + // Execute another task after the completion of runAsync() + System.out.println("Another task executed after runAsync() completes"); + }); + + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + // Perform result-producing task + return "Result of the asynchronous computation"; + }); + supplyAsyncFuture.thenApply(result -> { + // Transform the result + return result.toUpperCase(); + }) + .thenAccept(transformedResult -> { + // Consume the transformed result + System.out.println("Transformed Result: " + transformedResult); + }); + } +}