Java Reference
In-Depth Information
Table 9-7. Time required to calculate 10,000 mock price histories
Number of threads ThreadPoolExecutor ForkJoinPool
255.6 seconds
135.4 seconds
134.8 seconds
110.2 seconds
77.0 seconds
96.5 seconds
81.7 seconds
84.0 seconds
85.6 seconds
84.6 seconds
By default, the common pool will have four threads (on this four-CPU machine), so the third
line in the table is the common case. The results for a common pool size of one and two are
exactly the sort of result that should give a performance engineer fits: they seem to be com-
pletely out of line, and when a test is out of line like that, the most common reason is a test-
ing error. In this case, however, it turns out that the forEach() method does something
tricky: it uses both the thread executing the statement and the threads in the common pool to
process the data coming from the stream. Even though the common pool in the first test is
configured to have a single thread, two threads are used to calculate the result. (Conse-
quently, the time for a ThreadPoolExecutor with two threads and a ForkJoinPool with one
thread is essentially the same.)
If you need to tune the size of the common pool when using parallel stream constructs and
other autoparallel features, consider decreasing the desired value by one.
Search WWH ::

Custom Search