Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vsilaev committed Nov 8, 2018
2 parents 4f4a8b6 + e309da6 commit bd1b09e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ public @async CompletionStage<String> bar(int i) {
return async(result);
}
```
It's worth to mention, that when developing code with async/await you should avoid so-called ["async/await hell"](https://medium.com/@7genblogger/escaping-so-called-async-await-hell-in-node-js-b5f5ba5fa9ca). In short, pay special attention what parts of your code may be executed in parallel and what parts require serial execution. Consider the following example:
```java
public @async CompletionStage<Long> calculateTotalPrice(Order order) {
Long rawItemsPrice = await( calculateRawItemsPrice(order) );
Long shippingCost = await( calculateShippingCost(order) );
Long taxes = await( calculateTaxes(order) );
return async(rawItemsPrice + shippingCost + taxes);
}

protected @async CompletionStage<Long> calculateRawItemsPrice(Order order) {
...
}

protected @async CompletionStage<Long> calculateShippingCost(Order order) {
...
}

protected @async CompletionStage<Long> calculateTaxes(Order order) {
...
}
```
In the above example all async methods `calculateRawItemsPrice`, `calculateShippingCost`, `calculateTaxes` are executed serially, one by one, hence the performance is degraded comparing to the following parallelized solution:
```java
public @async CompletionStage<Long> calculateTotalPrice(Order order) {
CompletionStage<Long> rawItemsPrice = calculateRawItemsPrice(order);
CompletionStage<Long> shippingCost = calculateShippingCost(order);
CompletionStage<Long> taxes = calculateTaxes(order);
return async( await(rawItemsPrice) + await(shippingCost) + await(taxes) );
}
```
This way all inner async operations are started (almost) simualtenously and are running in parallel, unlike in the first example.

# Generators

Expand Down

0 comments on commit bd1b09e

Please sign in to comment.