为什么 ParallelFlux 没有类似于 Flux 的 collectList()?

问题描述 投票:0回答:1

Flux有collectList(),用起来很方便,但是ParallelFlux中没有collectList(),我想弄明白ParallelFlux中省略collectList()的原因

project-reactor
1个回答
4
投票

由于创建 ParallelFlux 是为了在不同线程中并行运行流,因此执行顺序与第一个流的顺序不同,因此 flux 不知道收集流的正确顺序。

  • 它提供给你

    collect()
    按照你的规则收集它

  • 它为您提供

    collectSortedList()
    以您选择的分类方式收集

  • 如果你只是想收集它作为一个

    List
    并且订单不计价你可以这样做

    Integer integer[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      ParallelFlux.from(Flux.fromArray(integer), 4)
              .runOn(Schedulers.parallel())
              .sequential()
              .collectList()
              .subscribe(integer1 -> System.out.println(integer1));
    
© www.soinside.com 2019 - 2024. All rights reserved.