mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-02 20:25:05 +00:00
docs: update async method invocation
This commit is contained in:
@@ -4,8 +4,10 @@ category: Concurrency
|
|||||||
language: en
|
language: en
|
||||||
tag:
|
tag:
|
||||||
- Asynchronous
|
- Asynchronous
|
||||||
|
- Decoupling
|
||||||
- Reactive
|
- Reactive
|
||||||
- Scalability
|
- Scalability
|
||||||
|
- Thread management
|
||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
@@ -18,9 +20,9 @@ Asynchronous method invocation is a pattern where the calling thread is not bloc
|
|||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
Real world example
|
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 the context of space rockets, an analogous example of the Async Method Invocation pattern can be seen in the communication between the mission control center and the onboard systems of the rocket. When mission control sends a command to the rocket to adjust its trajectory or perform a system check, they do not wait idly for the rocket to complete the task and report back. Instead, mission control continues to monitor other aspects of the mission and manage different tasks. The rocket executes the command asynchronously and sends a status update or result back to mission control once the operation is complete. This allows mission control to efficiently manage multiple concurrent operations without being blocked by any single task, similar to how asynchronous method invocation works in software systems.
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
@@ -112,7 +114,8 @@ public static void main(String[]args)throws Exception{
|
|||||||
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("Deploying lunar rover"));
|
final var asyncResult4 = executor.startProcess(lazyval(20, 400),
|
||||||
|
callback("Deploying lunar rover"));
|
||||||
final var asyncResult5 =
|
final var asyncResult5 =
|
||||||
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
|
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
|
||||||
|
|
||||||
@@ -128,15 +131,15 @@ final var result3=executor.endProcess(asyncResult3);
|
|||||||
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("Space rocket <"+result1+"> launch complete");
|
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result1));
|
||||||
log("Space rocket <"+result2+"> launch complete");
|
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result2));
|
||||||
log("Space rocket <"+result3+"> launch complete");
|
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result3));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Here's the program console output.
|
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.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.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.318[executor-4]INFO com.iluwatar.async.method.invocation.App-Space rocket<20>launched successfully
|
||||||
@@ -152,7 +155,7 @@ Here's the program console output.
|
|||||||
|
|
||||||
# Class diagram
|
# Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
@@ -196,4 +199,5 @@ Related Patterns:
|
|||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3Ti1N4f)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3Ti1N4f)
|
||||||
|
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||||
* [Java Concurrency in Practice](https://amzn.to/4ab97VU)
|
* [Java Concurrency in Practice](https://amzn.to/4ab97VU)
|
||||||
|
|||||||
Reference in New Issue
Block a user