Test environment: MacBook Pro 8-core.
Code
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)
.parallelStream()
.peek(i -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
})
stopWatch.stop();
System.out.println("result = " + stopWatch.getTotalTimeMillis());
In theory, one core allocates one thread. Since we need a total of 9 operations from 1 to 9, we need 9 threads.
That is, since the workload of 8 cores is exceeded, the result should be 2000ms, but 1000ms.
What's even more funny is that when I ran up to 16 threads, I still got 1000ms, and after 17, I got 2000ms. What's wrong?