File tree Expand file tree Collapse file tree 5 files changed +20
-5
lines changed
async-method-invocation/src/main/java/com/iluwatar/async/method/invocation Expand file tree Collapse file tree 5 files changed +20
-5
lines changed Original file line number Diff line number Diff line change 33import java .util .concurrent .Callable ;
44
55/**
6- * <p>
76 * This application demonstrates the async method invocation pattern. Key parts of the pattern are
87 * <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
98 * <code>AsyncCallback</code> which can be provided to be executed on task completion and
109 * <code>AsyncExecutor</code> that manages the execution of the async tasks.
11- * </p>
1210 * <p>
1311 * The main method shows example flow of async invocations. The main thread starts multiple tasks with
1412 * variable durations and then continues its own work. When the main thread has done it's job it collects
1513 * the results of the async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are
1614 * executed immediately when the tasks complete.
17- * </p>
1815 * <p>
1916 * Noteworthy difference of thread usage between the async results and callbacks is that the async results
2017 * are collected in the main thread but the callbacks are executed within the worker threads. This should be
2118 * noted when working with thread pools.
22- * </p>
2319 * <p>
2420 * Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture
2521 * and ExecutorService are the real world implementations of this pattern. But due to the nature of parallel
2622 * programming, the implementations are not trivial. This example does not take all possible scenarios into
2723 * account but rather provides a simple version that helps to understand the pattern.
28- * </p>
2924 *
3025 * @see AsyncResult
3126 * @see AsyncCallback
Original file line number Diff line number Diff line change 22
33import java .util .Optional ;
44
5+ /**
6+ *
7+ * AsyncCallback interface
8+ *
9+ * @param <T>
10+ *
11+ */
512public interface AsyncCallback <T > {
613
714 /**
Original file line number Diff line number Diff line change 33import java .util .concurrent .Callable ;
44import java .util .concurrent .ExecutionException ;
55
6+ /**
7+ *
8+ * AsyncExecutor interface
9+ *
10+ */
611public interface AsyncExecutor {
712
813 /**
Original file line number Diff line number Diff line change 22
33import java .util .concurrent .ExecutionException ;
44
5+ /**
6+ *
7+ * AsyncResult interface
8+ *
9+ * @param <T>
10+ */
511public interface AsyncResult <T > {
612
713 /**
Original file line number Diff line number Diff line change 66import java .util .concurrent .atomic .AtomicInteger ;
77
88/**
9+ *
910 * Implementation of async executor that creates a new thread for every task.
11+ *
1012 */
1113public class ThreadAsyncExecutor implements AsyncExecutor {
1214
You can’t perform that action at this time.
0 commit comments