|
| 1 | +package com.baeldung.async; |
| 2 | + |
| 3 | +import static com.ea.async.Async.await; |
| 4 | + |
| 5 | +import java.util.concurrent.Callable; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | +import java.util.concurrent.Future; |
| 11 | + |
| 12 | +import com.google.common.util.concurrent.AsyncCallable; |
| 13 | +import com.google.common.util.concurrent.Callables; |
| 14 | +import com.google.common.util.concurrent.Futures; |
| 15 | +import com.google.common.util.concurrent.ListenableFuture; |
| 16 | +import com.google.common.util.concurrent.ListeningExecutorService; |
| 17 | +import com.google.common.util.concurrent.MoreExecutors; |
| 18 | +import com.jcabi.aspects.Async; |
| 19 | +import com.jcabi.aspects.Loggable; |
| 20 | + |
| 21 | +public class JavaAsync { |
| 22 | + |
| 23 | + static { |
| 24 | + com.ea.async.Async.init(); |
| 25 | + } |
| 26 | + |
| 27 | + private static final ExecutorService threadpool = Executors.newCachedThreadPool(); |
| 28 | + |
| 29 | + public static void main (String[] args) throws InterruptedException, ExecutionException { |
| 30 | + int number = 20; |
| 31 | + |
| 32 | + //Thread Example |
| 33 | + factorialUsingThread(number).start(); |
| 34 | + |
| 35 | + //FutureTask Example |
| 36 | + Future<Long> futureTask = factorialUsingFutureTask(number); |
| 37 | + System.out.println("Factorial of " + number + " is: " + futureTask.get()); |
| 38 | + |
| 39 | + // CompletableFuture Example |
| 40 | + Future<Long> completableFuture = factorialUsingCompletableFuture(number); |
| 41 | + System.out.println("Factorial of " + number + " is: " + completableFuture.get()); |
| 42 | + |
| 43 | + // EA Async example |
| 44 | + System.out.println("Factorial of " + number + " is: " + factorialUsingEAAsync(number)); |
| 45 | + |
| 46 | + // cactoos async example |
| 47 | + Future<Long> asyncFuture = factorialUsingCactoos(number); |
| 48 | + System.out.println("Factorial of " + number + " is: " + asyncFuture.get()); |
| 49 | + |
| 50 | + // Guava example |
| 51 | + ListenableFuture<Long> guavaFuture = factorialUsingGuavaServiceSubmit(number); |
| 52 | + System.out.println("Factorial of " + number + " is: " + guavaFuture.get()); |
| 53 | + |
| 54 | + ListenableFuture<Long> guavaFutures = factorialUsingGuavaFutures(number); |
| 55 | + System.out.println("Factorial of " + number + " is: " + guavaFutures.get()); |
| 56 | + |
| 57 | + // @async jcabi-aspect example |
| 58 | + Future<Long> aspectFuture = factorialUsingJcabiAspect(number); |
| 59 | + System.out.println("Factorial of " + number + " is: " + aspectFuture.get()); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Finds factorial of a number |
| 65 | + * @param number |
| 66 | + * @return |
| 67 | + */ |
| 68 | + public static long factorial(int number) { |
| 69 | + long result = 1; |
| 70 | + for(int i=number;i>0;i--) { |
| 71 | + result *= i; |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Finds factorial of a number using Thread |
| 78 | + * @param number |
| 79 | + * @return |
| 80 | + */ |
| 81 | + @Loggable |
| 82 | + public static Thread factorialUsingThread(int number) { |
| 83 | + Thread newThread = new Thread(() -> { |
| 84 | + System.out.println("Factorial of " + number + " is: " + factorial(number)); |
| 85 | + }); |
| 86 | + |
| 87 | + return newThread; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Finds factorial of a number using FutureTask |
| 92 | + * @param number |
| 93 | + * @return |
| 94 | + */ |
| 95 | + @Loggable |
| 96 | + public static Future<Long> factorialUsingFutureTask(int number) { |
| 97 | + Future<Long> futureTask = threadpool.submit(() -> factorial(number)); |
| 98 | + |
| 99 | + while (!futureTask.isDone()) { |
| 100 | + System.out.println("FutureTask is not finished yet..."); |
| 101 | + } |
| 102 | + |
| 103 | + return futureTask; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Finds factorial of a number using CompletableFuture |
| 108 | + * @param number |
| 109 | + * @return |
| 110 | + */ |
| 111 | + @Loggable |
| 112 | + public static Future<Long> factorialUsingCompletableFuture(int number) { |
| 113 | + CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number)); |
| 114 | + return completableFuture; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Finds factorial of a number using EA Async |
| 119 | + * @param number |
| 120 | + * @return |
| 121 | + */ |
| 122 | + @Loggable |
| 123 | + public static long factorialUsingEAAsync(int number) { |
| 124 | + CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number)); |
| 125 | + long result = await(completableFuture); |
| 126 | + return result; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Finds factorial of a number using Async of Cactoos |
| 131 | + * @param number |
| 132 | + * @return |
| 133 | + * @throws InterruptedException |
| 134 | + * @throws ExecutionException |
| 135 | + */ |
| 136 | + @Loggable |
| 137 | + public static Future<Long> factorialUsingCactoos(int number) throws InterruptedException, ExecutionException { |
| 138 | + org.cactoos.func.Async<Integer, Long> asyncFunction = new org.cactoos.func.Async<Integer, Long>(input -> factorial(input)); |
| 139 | + Future<Long> asyncFuture = asyncFunction.apply(number); |
| 140 | + return asyncFuture; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Finds factorial of a number using Guava's ListeningExecutorService.submit() |
| 145 | + * @param number |
| 146 | + * @return |
| 147 | + */ |
| 148 | + @Loggable |
| 149 | + public static ListenableFuture<Long> factorialUsingGuavaServiceSubmit(int number) { |
| 150 | + ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool); |
| 151 | + ListenableFuture<Long> factorialFuture = (ListenableFuture<Long>) service.submit(()-> factorial(number)); |
| 152 | + return factorialFuture; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Finds factorial of a number using Guava's Futures.submitAsync() |
| 157 | + * @param number |
| 158 | + * @return |
| 159 | + */ |
| 160 | + @Loggable |
| 161 | + public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) { |
| 162 | + ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool); |
| 163 | + AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() { |
| 164 | + public Long call() { |
| 165 | + return factorial(number); |
| 166 | + } |
| 167 | + }, service); |
| 168 | + return Futures.submitAsync(asyncCallable, service); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Finds factorial of a number using @Async of jcabi-aspects |
| 173 | + * @param number |
| 174 | + * @return |
| 175 | + */ |
| 176 | + @Async |
| 177 | + @Loggable |
| 178 | + public static Future<Long> factorialUsingJcabiAspect(int number) { |
| 179 | + Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number)); |
| 180 | + return factorialFuture; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments