|
| 1 | +package com.iluwatar.async.method.invocation; |
| 2 | + |
| 3 | +import java.util.concurrent.Callable; |
| 4 | + |
| 5 | +/** |
| 6 | + * <p> |
| 7 | + * This application demonstrates the async method invocation pattern. Key parts of the pattern are |
| 8 | + * <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value, |
| 9 | + * <code>AsyncCallback</code> which can be provided to be executed on task completion and |
| 10 | + * <code>AsyncExecutor</code> that manages the execution of the async tasks. |
| 11 | + * </p> |
| 12 | + * <p> |
| 13 | + * The main method shows example flow of async invocations. The main thread starts multiple tasks with |
| 14 | + * variable durations and then continues its own work. When the main thread has done it's job it collects |
| 15 | + * the results of the async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are |
| 16 | + * executed immediately when the tasks complete. |
| 17 | + * </p> |
| 18 | + * <p> |
| 19 | + * Noteworthy difference of thread usage between the async results and callbacks is that the async results |
| 20 | + * are collected in the main thread but the callbacks are executed within the worker threads. This should be |
| 21 | + * noted when working with thread pools. |
| 22 | + * </p> |
| 23 | + * <p> |
| 24 | + * Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture |
| 25 | + * and ExecutorService are the real world implementations of this pattern. But due to the nature of parallel |
| 26 | + * programming, the implementations are not trivial. This example does not take all possible scenarios into |
| 27 | + * account but rather provides a simple version that helps to understand the pattern. |
| 28 | + * </p> |
| 29 | + * |
| 30 | + * @see AsyncResult |
| 31 | + * @see AsyncCallback |
| 32 | + * @see AsyncExecutor |
| 33 | + * |
| 34 | + * @see java.util.concurrent.FutureTask |
| 35 | + * @see java.util.concurrent.CompletableFuture |
| 36 | + * @see java.util.concurrent.ExecutorService |
| 37 | + */ |
| 38 | +public class App { |
| 39 | + |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + // construct a new executor that will run async tasks |
| 42 | + AsyncExecutor executor = new ThreadAsyncExecutor(); |
| 43 | + |
| 44 | + // start few async tasks with varying processing times, two last with callback handlers |
| 45 | + AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500)); |
| 46 | + AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300)); |
| 47 | + AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700)); |
| 48 | + AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4")); |
| 49 | + AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5")); |
| 50 | + |
| 51 | + // emulate processing in the current thread while async tasks are running in their own threads |
| 52 | + Thread.sleep(350); // Oh boy I'm working hard here |
| 53 | + log("Some hard work done"); |
| 54 | + |
| 55 | + // wait for completion of the tasks |
| 56 | + Integer result1 = executor.endProcess(asyncResult1); |
| 57 | + String result2 = executor.endProcess(asyncResult2); |
| 58 | + Long result3 = executor.endProcess(asyncResult3); |
| 59 | + asyncResult4.await(); |
| 60 | + asyncResult5.await(); |
| 61 | + |
| 62 | + // log the results of the tasks, callbacks log immediately when complete |
| 63 | + log("Result 1: " + result1); |
| 64 | + log("Result 2: " + result2); |
| 65 | + log("Result 3: " + result3); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a callable that lazily evaluates to given value with artificial delay. |
| 70 | + * |
| 71 | + * @param value value to evaluate |
| 72 | + * @param delayMillis artificial delay in milliseconds |
| 73 | + * @return new callable for lazy evaluation |
| 74 | + */ |
| 75 | + private static <T> Callable<T> lazyval(T value, long delayMillis) { |
| 76 | + return () -> { |
| 77 | + Thread.sleep(delayMillis); |
| 78 | + log("Task completed with: " + value); |
| 79 | + return value; |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a simple callback that logs the complete status of the async result. |
| 85 | + * |
| 86 | + * @param name callback name |
| 87 | + * @return new async callback |
| 88 | + */ |
| 89 | + private static <T> AsyncCallback<T> callback(String name) { |
| 90 | + return (value, ex) -> { |
| 91 | + if (ex.isPresent()) { |
| 92 | + log(name + " failed: " + ex.map(Exception::getMessage).orElse("")); |
| 93 | + } else { |
| 94 | + log(name + ": " + value); |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + private static void log(String msg) { |
| 100 | + System.out.println(String.format("[%1$-10s] - %2$s", Thread.currentThread().getName(), msg)); |
| 101 | + } |
| 102 | +} |
0 commit comments