BAEL-3596 - Asynchronous Method Calls in Java
This commit is contained in:
parent
0104b269fa
commit
9cd73de5cc
|
@ -1,37 +1,88 @@
|
|||
<?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>
|
||||
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>
|
||||
<name>java-async</name>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-aspects</artifactId>
|
||||
<version>0.22.6</version>
|
||||
</dependency>
|
||||
|
||||
<build>
|
||||
<finalName>java-async</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.9.5</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<properties>
|
||||
<decimal4j.version>1.0.3</decimal4j.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>28.1-jre</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.cactoos</groupId>
|
||||
<artifactId>cactoos</artifactId>
|
||||
<version>0.43</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ea.async</groupId>
|
||||
<artifactId>ea-async</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>java-async</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-maven-plugin</artifactId>
|
||||
<version>0.14.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>ajc</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjtools</artifactId>
|
||||
<version>1.9.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.async;
|
||||
|
||||
import static com.ea.async.Async.await;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import com.ea.async.Async;
|
||||
|
||||
public class EAAsyncExample {
|
||||
|
||||
static {
|
||||
Async.init();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
usingCompletableFuture();
|
||||
usingAsyncAwait();
|
||||
}
|
||||
|
||||
public static void usingCompletableFuture() throws InterruptedException, ExecutionException, Exception {
|
||||
CompletableFuture<Void> completableFuture = hello()
|
||||
.thenComposeAsync(hello -> mergeWorld(hello))
|
||||
.thenAcceptAsync(helloWorld -> print(helloWorld))
|
||||
.exceptionally( throwable -> {
|
||||
System.out.println(throwable.getCause());
|
||||
return null;
|
||||
});
|
||||
completableFuture.get();
|
||||
}
|
||||
|
||||
public static CompletableFuture<String> hello() {
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
public static CompletableFuture<String> mergeWorld(String s) {
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
return s + " World!";
|
||||
});
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
public static void print(String str) {
|
||||
CompletableFuture.runAsync(() -> System.out.println(str));
|
||||
}
|
||||
|
||||
private static void usingAsyncAwait() {
|
||||
try {
|
||||
String hello = await(hello());
|
||||
String helloWorld = await(mergeWorld(hello));
|
||||
await(CompletableFuture.runAsync(() -> print(helloWorld)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
package com.baeldung.async;
|
||||
|
||||
import static com.ea.async.Async.await;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.google.common.util.concurrent.AsyncCallable;
|
||||
import com.google.common.util.concurrent.Callables;
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import com.jcabi.aspects.Async;
|
||||
import com.jcabi.aspects.Loggable;
|
||||
|
||||
public class JavaAsync {
|
||||
|
||||
static {
|
||||
com.ea.async.Async.init();
|
||||
}
|
||||
|
||||
private static final ExecutorService threadpool = Executors.newCachedThreadPool();
|
||||
|
||||
public static void main (String[] args) throws InterruptedException, ExecutionException {
|
||||
int number = 20;
|
||||
|
||||
//Thread Example
|
||||
factorialUsingThread(number).start();
|
||||
|
||||
//FutureTask Example
|
||||
Future<Long> futureTask = factorialUsingFutureTask(number);
|
||||
System.out.println("Factorial of " + number + " is: " + futureTask.get());
|
||||
|
||||
// CompletableFuture Example
|
||||
Future<Long> completableFuture = factorialUsingCompletableFuture(number);
|
||||
System.out.println("Factorial of " + number + " is: " + completableFuture.get());
|
||||
|
||||
// EA Async example
|
||||
System.out.println("Factorial of " + number + " is: " + factorialUsingEAAsync(number));
|
||||
|
||||
// cactoos async example
|
||||
Future<Long> asyncFuture = factorialUsingCactoos(number);
|
||||
System.out.println("Factorial of " + number + " is: " + asyncFuture.get());
|
||||
|
||||
// Guava example
|
||||
ListenableFuture<Long> guavaFuture = factorialUsingGuavaServiceSubmit(number);
|
||||
System.out.println("Factorial of " + number + " is: " + guavaFuture.get());
|
||||
|
||||
ListenableFuture<Long> guavaFutures = factorialUsingGuavaFutures(number);
|
||||
System.out.println("Factorial of " + number + " is: " + guavaFutures.get());
|
||||
|
||||
// @async jcabi-aspect example
|
||||
Future<Long> aspectFuture = factorialUsingJcabiAspect(number);
|
||||
System.out.println("Factorial of " + number + " is: " + aspectFuture.get());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public static long factorial(int number) {
|
||||
long result = 1;
|
||||
for(int i=number;i>0;i--) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using Thread
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static Thread factorialUsingThread(int number) {
|
||||
Thread newThread = new Thread(() -> {
|
||||
System.out.println("Factorial of " + number + " is: " + factorial(number));
|
||||
});
|
||||
|
||||
return newThread;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using FutureTask
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static Future<Long> factorialUsingFutureTask(int number) {
|
||||
Future<Long> futureTask = threadpool.submit(() -> factorial(number));
|
||||
|
||||
while (!futureTask.isDone()) {
|
||||
System.out.println("FutureTask is not finished yet...");
|
||||
}
|
||||
|
||||
return futureTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using CompletableFuture
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static Future<Long> factorialUsingCompletableFuture(int number) {
|
||||
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using EA Async
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static long factorialUsingEAAsync(int number) {
|
||||
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
|
||||
long result = await(completableFuture);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Finds factorial of a number using Async of Cactoos
|
||||
* @param number
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
* @throws ExecutionException
|
||||
*/
|
||||
@Loggable
|
||||
public static Future<Long> factorialUsingCactoos(int number) throws InterruptedException, ExecutionException {
|
||||
org.cactoos.func.Async<Integer, Long> asyncFunction = new org.cactoos.func.Async<Integer, Long>(input -> factorial(input));
|
||||
Future<Long> asyncFuture = asyncFunction.apply(number);
|
||||
return asyncFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using Guava's ListeningExecutorService.submit()
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static ListenableFuture<Long> factorialUsingGuavaServiceSubmit(int number) {
|
||||
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
|
||||
ListenableFuture<Long> factorialFuture = (ListenableFuture<Long>) service.submit(()-> factorial(number));
|
||||
return factorialFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number using Guava's Futures.submitAsync()
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Loggable
|
||||
public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) {
|
||||
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
|
||||
AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() {
|
||||
public Long call() {
|
||||
return factorial(number);
|
||||
}
|
||||
}, service);
|
||||
return Futures.submitAsync(asyncCallable, service);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Finds factorial of a number using @Async of jcabi-aspects
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Async
|
||||
@Loggable
|
||||
public static Future<Long> factorialUsingJcabiAspect(int number) {
|
||||
Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number));
|
||||
return factorialFuture;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.baeldung.async;
|
||||
|
||||
public class AsyncUnitTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("hello");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue