Initial Commit

This commit is contained in:
Anshul BANSAL 2019-12-19 11:29:19 +02:00
parent 9cd4a0a16e
commit 0104b269fa
3 changed files with 92 additions and 0 deletions

37
java-async/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-async</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-async</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
</dependencies>
<build>
<finalName>java-async</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<decimal4j.version>1.0.3</decimal4j.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@ -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<String> 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<String> calculateAsync() throws InterruptedException {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
Thread.sleep(500);
completableFuture.complete("Hello");
return null;
});
return completableFuture;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.async;
public class AsyncUnitTest {
public static void main(String[] args) {
System.out.println("hello");
}
}