Java Reference
In-Depth Information
The CompletableFuture version seems just a bit faster than the one using parallel stream. But
this last version isn't satisfying either. For instance, if you try to run your code with nine shops,
the parallel stream version takes 3143 milliseconds, whereas the CompletableFuture one
requires 3009 milliseconds. They look equivalent and for a very good reason: they both
internally use the same common pool that by default has a fixed number of threads equal to the
one returned by Runtime.getRuntime() .availableProcessors(). Nevertheless,
CompletableFutures have an advantage because, in contrast to what's offered by the parallel
Streams API, they allow you to specify a different Executor to submit their tasks to. This allows
you to configure this Executor, and in particular to size its thread pool, in a way that better fits
the requirements of your application. Let's see if you can translate this better level of
configurability into practical performance gain for your application.
11.3.4. Using a custom Executor
In this case, a sensible choice seems to be to create an Executor with a number of threads in its
pool that takes into account the actual workload you could expect in your application, but how
do you correctly size it?
Sizing thread pools
In the great book Java Concurrency in Practice ( http://mng.bz/979c ) , Brian Goetz and
coauthors give some advice to find the optimal size for a thread pool. This is important because
if the number of threads in the pool is too big, they'll end up competing for scarce CPU and
memory resources, wasting their time performing context switching. Conversely, if this number
is too small (as it very likely is in your application), some of the cores of the CPU will remain
underutilized. In particular, Goetz suggests that the right pool size to approximate a desired
CPU utilization rate can be calculated with the following formula:
N threads = N CPU * U CPU * (1 + W/C)
where
N CPU is the number of cores, available through Runtime.getRuntime().availableProcessors()
U CPU is the target CPU utilization (between 0 and 1), and
W/C is the ratio of wait time to compute time
Search WWH ::




Custom Search