Reformat Async Method Invocation - Issue #224
This commit is contained in:
parent
95c16200e7
commit
16a8c85af6
async-method-invocation/src
main/java/com/iluwatar/async/method/invocation
test/java/com/iluwatar/async/method/invocation
@ -4,23 +4,24 @@ import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
|
||||
* <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
|
||||
* value, <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
||||
* <p>
|
||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks with
|
||||
* variable durations and then continues its own work. When the main thread has done it's job it collects
|
||||
* the results of the async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are
|
||||
* executed immediately when the tasks complete.
|
||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks
|
||||
* with variable durations and then continues its own work. When the main thread has done it's job
|
||||
* it collects the results of the async tasks. Two of the tasks are handled with callbacks, meaning
|
||||
* the callbacks are executed immediately when the tasks complete.
|
||||
* <p>
|
||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async results
|
||||
* are collected in the main thread but the callbacks are executed within the worker threads. This should be
|
||||
* noted when working with thread pools.
|
||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async
|
||||
* results are collected in the main thread but the callbacks are executed within the worker
|
||||
* threads. This should be noted when working with thread pools.
|
||||
* <p>
|
||||
* Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture
|
||||
* and ExecutorService are the real world implementations of this pattern. But due to the nature of parallel
|
||||
* programming, the implementations are not trivial. This example does not take all possible scenarios into
|
||||
* account but rather provides a simple version that helps to understand the pattern.
|
||||
* Java provides its own implementations of async method invocation pattern. FutureTask,
|
||||
* CompletableFuture and ExecutorService are the real world implementations of this pattern. But due
|
||||
* to the nature of parallel programming, the implementations are not trivial. This example does not
|
||||
* take all possible scenarios into account but rather provides a simple version that helps to
|
||||
* understand the pattern.
|
||||
*
|
||||
* @see AsyncResult
|
||||
* @see AsyncCallback
|
||||
@ -40,8 +41,10 @@ public class App {
|
||||
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
AsyncResult<Integer> asyncResult4 =
|
||||
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 =
|
||||
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
|
||||
// emulate processing in the current thread while async tasks are running in their own threads
|
||||
Thread.sleep(350); // Oh boy I'm working hard here
|
||||
|
@ -18,5 +18,4 @@ public interface AsyncCallback<T> {
|
||||
* @param ex empty value if execution succeeds, some exception if executions fails
|
||||
*/
|
||||
void onComplete(T value, Optional<Exception> ex);
|
||||
|
||||
}
|
||||
|
@ -38,5 +38,4 @@ public interface AsyncExecutor {
|
||||
* @throws InterruptedException if the execution is interrupted
|
||||
*/
|
||||
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
|
||||
|
||||
}
|
||||
|
15
async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java
15
async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java
@ -34,7 +34,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
|
||||
InterruptedException {
|
||||
if (asyncResult.isCompleted()) {
|
||||
return asyncResult.getValue();
|
||||
} else {
|
||||
@ -44,8 +45,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple implementation of async result that allows completing it successfully with a value
|
||||
* or exceptionally with an exception. A really simplified version from its real life cousins
|
||||
* Simple implementation of async result that allows completing it successfully with a value or
|
||||
* exceptionally with an exception. A really simplified version from its real life cousins
|
||||
* FutureTask and CompletableFuture.
|
||||
*
|
||||
* @see java.util.concurrent.FutureTask
|
||||
@ -70,8 +71,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value from successful execution and executes callback if available. Notifies
|
||||
* any thread waiting for completion.
|
||||
* Sets the value from successful execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
*
|
||||
* @param value value of the evaluated task
|
||||
*/
|
||||
@ -85,8 +86,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies
|
||||
* any thread waiting for completion.
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
*
|
||||
* @param exception exception of the failed task
|
||||
*/
|
||||
|
@ -14,5 +14,4 @@ public class AppTest {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user