Related: CompletableFuture on ParallelStream gets batched and runs slower than sequential stream?
I'm doing some research on different ways of parallelizing network calls through parallelStream and CompletableFutures. As such, I have come across this situation where the ForkJoinPool.commonPool(), which is used by java's parallelStream, is dynamically growing in size, from ~ #Cores, to Max value of 64.
Java details:
$ java -version
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)
Code that shows such behavior is below (Full executable code here)
public static int loops = 100;
private static long sleepTimeMs = 1000;
private static ExecutorService customPool = Executors.newFixedThreadPool(loops);
// this method shows dynamic increase in pool size
public static void m1() {
Instant start = Instant.now();
LongSummaryStatistics stats = LongStream.range(0, loops).boxed()
.parallel()
.map(number -> CompletableFuture.supplyAsync(
() -> DummyProcess.slowNetworkCall(number), customPool))
.map(CompletableFuture::join)
.mapToLong(Long::longValue)
.summaryStatistics();
}
// this method shows static pool size
public static void m2() {
Instant start = Instant.now();
LongSummaryStatistics stats = LongStream.range(0, loops)
.parallel()
.map(DummyProcess::slowNetworkCall) // in this call, parallelism/poolsize stays constant 11
.summaryStatistics();
}
public static Long slowNetworkCall(Long i) {
Instant start = Instant.now();
// starts with 11 (#cores in my laptop = 12), goes upto 64
log.info(" {} going to sleep. poolsize: {}", i, ForkJoinPool.commonPool().getPoolSize());
try {
TimeUnit.MILLISECONDS.sleep(sleepTimeMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(" {} woke up..", i);
return Duration.between(start, Instant.now()).toMillis();
}
Sample output:
16:07:17.443 [pool-2-thread-7] INFO generalworks.parallelism.DummyProcess - 44 going to sleep. poolsize: 11
16:07:17.443 [pool-2-thread-9] INFO generalworks.parallelism.DummyProcess - 7 going to sleep. poolsize: 12
16:07:17.443 [pool-2-thread-4] INFO generalworks.parallelism.DummyProcess - 6 going to sleep. poolsize: 12
16:07:17.444 [pool-2-thread-13] INFO generalworks.parallelism.DummyProcess - 82 going to sleep. poolsize: 13
16:07:17.444 [pool-2-thread-14] INFO generalworks.parallelism.DummyProcess - 26 going to sleep. poolsize: 14
16:07:17.444 [pool-2-thread-15] INFO generalworks.parallelism.DummyProcess - 96 going to sleep. poolsize: 15
16:07:17.445 [pool-2-thread-16] INFO generalworks.parallelism.DummyProcess - 78 going to sleep. poolsize: 16
.
.
16:07:18.460 [pool-2-thread-79] INFO generalworks.parallelism.DummyProcess - 2 going to sleep. poolsize: 64
16:07:18.460 [pool-2-thread-71] INFO generalworks.parallelism.DummyProcess - 36 going to sleep. poolsize: 64
16:07:18.460 [pool-2-thread-74] INFO generalworks.parallelism.DummyProcess - 77 going to sleep. poolsize: 64
16:07:18.461 [pool-2-thread-83] INFO generalworks.parallelism.DummyProcess - 86 going to sleep. poolsize: 64
I understand that the number of Threads in a commonpool, i.e, it parallelism
is based upon max number of available cores, so since my laptop has 12 cores, i get a parallelism of 11 to start with. But I do not understand why it keeps climbing in one method, but in the other one, it's size keeps constants
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…