Java Reference
In-Depth Information
parallel stream. Moreover, it's even more disappointing considering you obtained the parallel
stream version with a trivial change to the sequential version.
In comparison, our newer version using CompletableFutures required quite a bit of work! But is
this the whole truth? Is using CompletableFutures in this scenario really a waste of time? Or are
we perhaps overlooking something important? Take a few minutes before going forward,
particularly recalling that you're testing the code samples on a machine capable of running four
threads in parallel. [ 1 ]
1 If you're using a machine capable of running more threads in parallel (for example, eight), then
it will require more shops and processes in parallel to reproduce the behavior shown in these
pages.
11.3.3. Looking for the solution that scales better
The parallel stream version performs so well only because it can run four tasks in parallel, so it's
able to allocate exactly one thread for each shop. But what happens if you decide to add a fifth
shop to the list of shops crawled by your best-price-finder application? Not surprisingly, now the
sequential version requires just a bit more than 5 seconds to run, as shown in the following
output:
Unfortunately, the parallel stream version will also now require a whole second more than
before, because all four threads it can run in parallel (available in the common thread pool) are
now busy with the first four shops. The fifth query will have to wait for the completion of one of
the former operations to free up a thread, as shown here:
What about the CompletableFuture version? Let's also give it a try with the additional fifth shop:
 
Search WWH ::




Custom Search