Performance difference on 2 different streams executions
I try to do the same operation for default parallel stream and using custom ForkJoin pool.
I see huge performance difference for the same operation. 94 ms vs ~5341 ms (Time1 and Time2 are almost the same - so I don't blame awaitQuiescence here)
What can be a reason ? Tricky java intrinsic ?
public final class SharedForkJoinExecutor {
        private static final Logger LOGGER = LoggerFactory.getLogger(SharedForkJoinExecutor.class);
    private static final ForkJoinPool EXEC = new ForkJoinPool(ForkJoinPool.commonPool().getParallelism(),
                                                              pool -> {
                                                                  final ForkJoinWorkerThread aThread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
                                                                  aThread.setName("ForkJoin-Executor-" + aThread.getPoolIndex());
                                                                  return aThread;
                                                              },
                                                              (t, e) -> LOGGER.info(e.getMessage(), e),
                                                              true);
    /**
     * Shuts down this executor
     */
    public static void shutdown() {
        EXEC.shutdown();
    }
    public static ForkJoinPool get() {
        return EXEC;
    }
}
package com.stream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import com.stream.SharedForkJoinExecutor;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class ForkJoinTest {
    private static final int INT_NUMBERS = 1_000_000;
    @Test
    public void forEachIntTest() {
        final AtomicInteger aEvenCounter = new AtomicInteger(0);
        final AtomicInteger aAllCounter = new AtomicInteger(0);
        long t = System.currentTimeMillis();
        IntStream.range(0, INT_NUMBERS).parallel().forEach(theIndex -> {
            if (theIndex % 2 == 0) {
                aEvenCounter.incrementAndGet();
            }
            aAllCounter.incrementAndGet();
        });
        System.out.println("Time=" + (System.currentTimeMillis() - t));
        assertEquals(INT_NUMBERS / 2, aEvenCounter.get());
        assertEquals(INT_NUMBERS, aAllCounter.get());
        aEvenCounter.set(0);
        aAllCounter.set(0);
        t = System.currentTimeMillis();
        SharedForkJoinExecutor.get().execute(() -> IntStream.range(0, INT_NUMBERS).parallel().forEach(theIndex -> {
            if (theIndex % 2 == 0) {
                aEvenCounter.incrementAndGet();
            }
            aAllCounter.incrementAndGet();
        }));
        System.out.println("Time1=" + (System.currentTimeMillis() - t));
        SharedForkJoinExecutor.get().awaitQuiescence(10, TimeUnit.HOURS);
        System.out.println("Time2=" + (System.currentTimeMillis() - t));
        assertEquals(INT_NUMBERS / 2, aEvenCounter.get());
        assertEquals(INT_NUMBERS, aAllCounter.get());
    }
}
 
    