Java Reference
In-Depth Information
As you may imagine, the performance of this implementation will be far from optimal, but try to
measure it, as usual, by running your benchmark:
[BestPrice price is 110.93, LetsSaveBig price is 135.58, MyFavoriteShop price is 192.72, BuyItAll
price is 184.74, ShopEasy price is 167.28]
Done in 10028 msecs
As expected, it takes 10 seconds, because the 5 seconds used in sequentially querying the five
shops is now added to the 5 seconds consumed by the discount service to apply the discount
code to the prices returned by the five shops. You already know you can easily improve this
result by converting the stream into a parallel one. However, you also learned in section 11.3
that this solution doesn't scale very well when you increase the number of shops to be queried,
due to the fixed common thread pool that streams rely on. Conversely, you learned that you
could better utilize your CPU by defining a custom Executor that will schedule the tasks
performed by the CompletableFutures.
11.4.3. Composing synchronous and asynchronous operations
Let's try to reimplement the findPrices method asynchronously, again using the features
provided by CompletableFuture. Here's the code for it. Don't worry if there's something that
looks unfamiliar; we explain it shortly.
Listing 11.16. Implementing the findPrices method with
CompletableFuture s
 
Search WWH ::




Custom Search