forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#8456 from SmartyAnsh/BAEL-3596_Asynchronous…
…_Method_Calls_in_Java Bael 3596 asynchronous method calls in java
- Loading branch information
Showing
3 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...les/core-java-concurrency-advanced-3/src/main/java/com/baeldung/async/EAAsyncExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
183 changes: 183 additions & 0 deletions
183
...-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/async/JavaAsync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
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.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; | ||
} | ||
|
||
} |