Multithreading ExecutorService在使用流时不并行执行线程<;未来>;

Multithreading ExecutorService在使用流时不并行执行线程<;未来>;,multithreading,java-8,Multithreading,Java 8,我使用的是ExecutionService,如下所示: ExecutorService exe = Executors.newWorkStealingPool(parts.size()); ... Stream<Future<String>> futures = parts.stream().map(part -> exe.submit(() -> processPartition(part))); ... String

我使用的是
ExecutionService
,如下所示:

    ExecutorService exe = Executors.newWorkStealingPool(parts.size());
    ...
    Stream<Future<String>> futures = parts.stream().map(part -> exe.submit(() -> processPartition(part)));
    ...
    String ret[] = futures.map(t -> {
        try {
            return t.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }).toArray(n -> new String[n]);
ExecutorService exe=Executors.newWorkStealingPool(parts.size());
...
Stream futures=parts.Stream().map(part->exe.submit(()->processPartition(part));
...
字符串ret[]=futures.map(t->{
试一试{
返回t.get();
}捕获(中断异常|执行异常e){
抛出新的运行时异常(e);
}
}).toArray(n->新字符串[n]);
processPartition()
中的代码一次只执行一个


发生了什么事?

我花了几个小时对此进行故障排除,最后在发布2分钟后找到了答案

问题在于这种模式:

Stream<Future<String>> futures = [...]
这将强制所有线程都被提交。

请注意存在。顺便说一下,
.toArray(n->newstring[n])
可以写成
.toArray(String[]::new)
List<Future<String>> futures = [...] .collect(Collectors.toList());