Add explanation to Async Method Invocation pattern (#1680)
* #590 make the example more interesting and add an explanation * #590 fix checkstyle Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
This commit is contained in:
parent
e9106ccfc5
commit
7e91322a43
@ -9,21 +9,160 @@ tags:
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
Asynchronous method invocation is pattern where the calling thread
|
|
||||||
|
Asynchronous method invocation is a pattern where the calling thread
|
||||||
is not blocked while waiting results of tasks. The pattern provides parallel
|
is not blocked while waiting results of tasks. The pattern provides parallel
|
||||||
processing of multiple independent tasks and retrieving the results via
|
processing of multiple independent tasks and retrieving the results via
|
||||||
callbacks or waiting until everything is done.
|
callbacks or waiting until everything is done.
|
||||||
|
|
||||||
|
## Explanation
|
||||||
|
|
||||||
|
Real world example
|
||||||
|
|
||||||
|
> Launching space rockets is an exciting business. The mission command gives an order to launch and
|
||||||
|
> after some undetermined time, the rocket either launches successfully or fails miserably.
|
||||||
|
|
||||||
|
In plain words
|
||||||
|
|
||||||
|
> Asynchronous method invocation starts task processing and returns immediately before the task is
|
||||||
|
> ready. The results of the task processing are returned to the caller later.
|
||||||
|
|
||||||
|
Wikipedia says
|
||||||
|
|
||||||
|
> In multithreaded computer programming, asynchronous method invocation (AMI), also known as
|
||||||
|
> asynchronous method calls or the asynchronous pattern is a design pattern in which the call site
|
||||||
|
> is not blocked while waiting for the called code to finish. Instead, the calling thread is
|
||||||
|
> notified when the reply arrives. Polling for a reply is an undesired option.
|
||||||
|
|
||||||
|
**Programmatic Example**
|
||||||
|
|
||||||
|
In this example, we are launching space rockets and deploying lunar rovers.
|
||||||
|
|
||||||
|
The application demonstrates the async method invocation pattern. The key parts of the pattern are
|
||||||
|
`AsyncResult` which is an intermediate container for an asynchronously evaluated value,
|
||||||
|
`AsyncCallback` which can be provided to be executed on task completion and `AsyncExecutor` that
|
||||||
|
manages the execution of the async tasks.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public interface AsyncResult<T> {
|
||||||
|
boolean isCompleted();
|
||||||
|
T getValue() throws ExecutionException;
|
||||||
|
void await() throws InterruptedException;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
public interface AsyncCallback<T> {
|
||||||
|
void onComplete(T value, Optional<Exception> ex);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
public interface AsyncExecutor {
|
||||||
|
<T> AsyncResult<T> startProcess(Callable<T> task);
|
||||||
|
<T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback);
|
||||||
|
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`ThreadAsyncExecutor` is an implementation of `AsyncExecutor`. Some of its key parts are highlighted
|
||||||
|
next.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> AsyncResult<T> startProcess(Callable<T> task) {
|
||||||
|
return startProcess(task, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
|
||||||
|
var result = new CompletableResult<>(callback);
|
||||||
|
new Thread(
|
||||||
|
() -> {
|
||||||
|
try {
|
||||||
|
result.setValue(task.call());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
result.setException(ex);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"executor-" + idx.incrementAndGet())
|
||||||
|
.start();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T endProcess(AsyncResult<T> asyncResult)
|
||||||
|
throws ExecutionException, InterruptedException {
|
||||||
|
if (!asyncResult.isCompleted()) {
|
||||||
|
asyncResult.await();
|
||||||
|
}
|
||||||
|
return asyncResult.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then we are ready to launch some rockets to see how everything works together.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// construct a new executor that will run async tasks
|
||||||
|
var executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
// start few async tasks with varying processing times, two last with callback handlers
|
||||||
|
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||||
|
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||||
|
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||||
|
final var asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Deploying lunar rover"));
|
||||||
|
final var asyncResult5 =
|
||||||
|
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
|
||||||
|
|
||||||
|
// emulate processing in the current thread while async tasks are running in their own threads
|
||||||
|
Thread.sleep(350); // Oh boy, we are working hard here
|
||||||
|
log("Mission command is sipping coffee");
|
||||||
|
|
||||||
|
// wait for completion of the tasks
|
||||||
|
final var result1 = executor.endProcess(asyncResult1);
|
||||||
|
final var result2 = executor.endProcess(asyncResult2);
|
||||||
|
final var result3 = executor.endProcess(asyncResult3);
|
||||||
|
asyncResult4.await();
|
||||||
|
asyncResult5.await();
|
||||||
|
|
||||||
|
// log the results of the tasks, callbacks log immediately when complete
|
||||||
|
log("Space rocket <" + result1 + "> launch complete");
|
||||||
|
log("Space rocket <" + result2 + "> launch complete");
|
||||||
|
log("Space rocket <" + result3 + "> launch complete");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's the program console output.
|
||||||
|
|
||||||
|
```java
|
||||||
|
21:47:08.227 [executor-2] INFO com.iluwatar.async.method.invocation.App - Space rocket <test> launched successfully
|
||||||
|
21:47:08.269 [main] INFO com.iluwatar.async.method.invocation.App - Mission command is sipping coffee
|
||||||
|
21:47:08.318 [executor-4] INFO com.iluwatar.async.method.invocation.App - Space rocket <20> launched successfully
|
||||||
|
21:47:08.335 [executor-4] INFO com.iluwatar.async.method.invocation.App - Deploying lunar rover <20>
|
||||||
|
21:47:08.414 [executor-1] INFO com.iluwatar.async.method.invocation.App - Space rocket <10> launched successfully
|
||||||
|
21:47:08.519 [executor-5] INFO com.iluwatar.async.method.invocation.App - Space rocket <callback> launched successfully
|
||||||
|
21:47:08.519 [executor-5] INFO com.iluwatar.async.method.invocation.App - Deploying lunar rover <callback>
|
||||||
|
21:47:08.616 [executor-3] INFO com.iluwatar.async.method.invocation.App - Space rocket <50> launched successfully
|
||||||
|
21:47:08.617 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <10> launch complete
|
||||||
|
21:47:08.617 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <test> launch complete
|
||||||
|
21:47:08.618 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <50> launch complete
|
||||||
|
```
|
||||||
|
|
||||||
# Class diagram
|
# Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Use async method invocation pattern when
|
|
||||||
|
Use the async method invocation pattern when
|
||||||
|
|
||||||
* You have multiple independent tasks that can run in parallel
|
* You have multiple independent tasks that can run in parallel
|
||||||
* You need to improve the performance of a group of sequential tasks
|
* You need to improve the performance of a group of sequential tasks
|
||||||
* You have limited amount of processing capacity or long running tasks and the
|
* You have a limited amount of processing capacity or long-running tasks and the caller should not wait for the tasks to be ready
|
||||||
caller should not wait the tasks to be ready
|
|
||||||
|
|
||||||
## Real world examples
|
## Real world examples
|
||||||
|
|
||||||
|
@ -27,10 +27,12 @@ import java.util.concurrent.Callable;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
* In this example, we are launching space rockets and deploying lunar rovers.
|
||||||
* <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
|
* <p>The application demonstrates the async method invocation pattern. The key parts of the
|
||||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
* 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>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
|
* <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
|
* tasks with variable durations and then continues its own work. When the main thread has done it's
|
||||||
@ -68,13 +70,14 @@ public class App {
|
|||||||
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
|
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||||
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
|
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||||
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||||
final var asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
final var asyncResult4 = executor.startProcess(lazyval(20, 400),
|
||||||
|
callback("Deploying lunar rover"));
|
||||||
final var asyncResult5 =
|
final var asyncResult5 =
|
||||||
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
|
||||||
|
|
||||||
// emulate processing in the current thread while async tasks are running in their own threads
|
// 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
|
Thread.sleep(350); // Oh boy, we are working hard here
|
||||||
log("Some hard work done");
|
log("Mission command is sipping coffee");
|
||||||
|
|
||||||
// wait for completion of the tasks
|
// wait for completion of the tasks
|
||||||
final var result1 = executor.endProcess(asyncResult1);
|
final var result1 = executor.endProcess(asyncResult1);
|
||||||
@ -84,9 +87,9 @@ public class App {
|
|||||||
asyncResult5.await();
|
asyncResult5.await();
|
||||||
|
|
||||||
// log the results of the tasks, callbacks log immediately when complete
|
// log the results of the tasks, callbacks log immediately when complete
|
||||||
log("Result 1: " + result1);
|
log("Space rocket <" + result1 + "> launch complete");
|
||||||
log("Result 2: " + result2);
|
log("Space rocket <" + result2 + "> launch complete");
|
||||||
log("Result 3: " + result3);
|
log("Space rocket <" + result3 + "> launch complete");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -99,7 +102,7 @@ public class App {
|
|||||||
private static <T> Callable<T> lazyval(T value, long delayMillis) {
|
private static <T> Callable<T> lazyval(T value, long delayMillis) {
|
||||||
return () -> {
|
return () -> {
|
||||||
Thread.sleep(delayMillis);
|
Thread.sleep(delayMillis);
|
||||||
log("Task completed with: " + value);
|
log("Space rocket <" + value + "> launched successfully");
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -115,7 +118,7 @@ public class App {
|
|||||||
if (ex.isPresent()) {
|
if (ex.isPresent()) {
|
||||||
log(name + " failed: " + ex.map(Exception::getMessage).orElse(""));
|
log(name + " failed: " + ex.map(Exception::getMessage).orElse(""));
|
||||||
} else {
|
} else {
|
||||||
log(name + ": " + value);
|
log(name + " <" + value + ">");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user