Custom thread pool in Java 8 parallel stream

后端 未结 15 926
旧巷少年郎
旧巷少年郎 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 01:11

    If you don't need a custom ThreadPool but you rather want to limit the number of concurrent tasks, you can use:

    List paths = List.of("/path/file1.csv", "/path/file2.csv", "/path/file3.csv").stream().map(e -> Paths.get(e)).collect(toList());
    List> partitions = Lists.partition(paths, 4); // Guava method
    
    partitions.forEach(group -> group.parallelStream().forEach(csvFilePath -> {
           // do your processing   
    }));
    

    (Duplicate question asking for this is locked, so please bear me here)

提交回复
热议问题