Java Reference
In-Depth Information
Figure 11.4. Why Stream's laziness causes a sequential computation and
how to avoid it
The top half of figure 11.4 shows that processing the stream with a single pipeline implies the
evaluation order (identified by the dotted line) is sequential. In fact, a new CompletableFuture is
created only after the former one has been completely evaluated. Conversely, the bottom half of
the figure demonstrates how gathering the CompletableFutures in a list first, represented by the
oval, allows all of them to start before waiting for their completion.
Running the code in listing 11.11 to check the performance of this third version of the findPrices
method, you could obtain output along the lines of this:
[BestPrice price is 123.26, LetsSaveBig price is 169.47, MyFavoriteShop price is 214.13, BuyItAll
price is 184.74]
Done in 2005 msecs
This is quite disappointing, isn't it? More than 2 seconds means this implementation using
CompletableFutures is faster than the original naïve sequential and blocking implementation
from listing 11.8 . But it's also almost twice as slow as the previous implementation using a
 
Search WWH ::




Custom Search