From 0104b269fac0399e44d401f2895a5dff0b14a06d Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Thu, 19 Dec 2019 11:29:19 +0200 Subject: [PATCH] Initial Commit --- java-async/pom.xml | 37 +++++++++++++++ .../main/java/com/baeldung/async/Async.java | 46 +++++++++++++++++++ .../com/baeldung/async/AsyncUnitTest.java | 9 ++++ 3 files changed, 92 insertions(+) create mode 100644 java-async/pom.xml create mode 100644 java-async/src/main/java/com/baeldung/async/Async.java create mode 100644 java-async/src/main/resources/com/baeldung/async/AsyncUnitTest.java diff --git a/java-async/pom.xml b/java-async/pom.xml new file mode 100644 index 0000000000..56ff53321f --- /dev/null +++ b/java-async/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + java-async + 0.1.0-SNAPSHOT + java-async + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + + + java-async + + + src/main/resources + true + + + + + + 1.0.3 + 3.6.1 + + + diff --git a/java-async/src/main/java/com/baeldung/async/Async.java b/java-async/src/main/java/com/baeldung/async/Async.java new file mode 100644 index 0000000000..b505077d10 --- /dev/null +++ b/java-async/src/main/java/com/baeldung/async/Async.java @@ -0,0 +1,46 @@ +package com.baeldung.async; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class Async { + + public static void main(String[] args) { + System.out.println("hello world!!"); + + try { + Future completableFuture = calculateAsync(); + + String result = completableFuture.get(); + + System.out.println(result); + + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + CompletableFuture.runAsync(() -> { + System.out.println("This is Async"); + }); + } + + public static Future calculateAsync() throws InterruptedException { + CompletableFuture completableFuture = new CompletableFuture<>(); + + Executors.newCachedThreadPool().submit(() -> { + Thread.sleep(500); + completableFuture.complete("Hello"); + return null; + }); + + return completableFuture; + } + +} \ No newline at end of file diff --git a/java-async/src/main/resources/com/baeldung/async/AsyncUnitTest.java b/java-async/src/main/resources/com/baeldung/async/AsyncUnitTest.java new file mode 100644 index 0000000000..57a61dbfe2 --- /dev/null +++ b/java-async/src/main/resources/com/baeldung/async/AsyncUnitTest.java @@ -0,0 +1,9 @@ +package com.baeldung.async; + +public class AsyncUnitTest { + + public static void main(String[] args) { + System.out.println("hello"); + } + +} \ No newline at end of file