Custom thread pool in Java 8 parallel stream

后端 未结 15 1157
旧巷少年郎
旧巷少年郎 2020-11-22 00:15

Is it possible to specify a custom thread pool for Java 8 parallel stream? I can not find it anywhere.

Imagine that I have a server application and I would like to

15条回答
  •  失恋的感觉
    2020-11-22 00:55

    I tried the custom ForkJoinPool as follows to adjust the pool size:

    private static Set ThreadNameSet = new HashSet<>();
    private static Callable getSum() {
        List aList = LongStream.rangeClosed(0, 10_000_000).boxed().collect(Collectors.toList());
        return () -> aList.parallelStream()
                .peek((i) -> {
                    String threadName = Thread.currentThread().getName();
                    ThreadNameSet.add(threadName);
                })
                .reduce(0L, Long::sum);
    }
    
    private static void testForkJoinPool() {
        final int parallelism = 10;
    
        ForkJoinPool forkJoinPool = null;
        Long result = 0L;
        try {
            forkJoinPool = new ForkJoinPool(parallelism);
            result = forkJoinPool.submit(getSum()).get(); //this makes it an overall blocking call
    
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            if (forkJoinPool != null) {
                forkJoinPool.shutdown(); //always remember to shutdown the pool
            }
        }
        out.println(result);
        out.println(ThreadNameSet);
    }
    

    Here is the output saying the pool is using more threads than the default 4.

    50000005000000
    [ForkJoinPool-1-worker-8, ForkJoinPool-1-worker-9, ForkJoinPool-1-worker-6, ForkJoinPool-1-worker-11, ForkJoinPool-1-worker-10, ForkJoinPool-1-worker-1, ForkJoinPool-1-worker-15, ForkJoinPool-1-worker-13, ForkJoinPool-1-worker-4, ForkJoinPool-1-worker-2]
    

    But actually there is a weirdo, when I tried to achieve the same result using ThreadPoolExecutor as follows:

    BlockingDeque blockingDeque = new LinkedBlockingDeque(1000);
    ThreadPoolExecutor fixedSizePool = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, blockingDeque, new MyThreadFactory("my-thread"));
    

    but I failed.

    It will only start the parallelStream in a new thread and then everything else is just the same, which again proves that the parallelStream will use the ForkJoinPool to start its child threads.

提交回复
热议问题