项目反应堆中的Flatmap,Flatmap序列和辅助图有什么区别?

发布于 2025-01-23 10:21:39 字数 832 浏览 3 评论 0原文

我从文档中读到flatmap

将这种通量异步发出的元素转化为发布者,然后通过合并将这些内部发行商弄平为单个通量,从而使它们交织在一起。

FlatMapSequention

将这种通量异步发出的元素转换为发布者,然后将这些内部发行商弄平为单个通量,但按照其源元素的顺序合并。

concatmap

将这种通量异步发出的元素转化为发布者,然后将这些内部发行商弄平为单个磁通,并使用串联依次保存顺序。 该操作员有三个维度可以与flatmap和flatmap序列进行比较:

生成内部和订阅:该操作员在生成下一个并订阅下来之前等待一个内部完成。

扁平值的排序:该操作员自然保持与源元素相同的顺序,从而依次从每个源元素中加入Inners。

交错:该操作员不允许不同内部的价值交织(串联)。

flatmap与其他两个之间的差异是可以理解的,但是我不明白concatmapflatmapsequepential之间的差异。两者之间有任何性能差异吗?我已经读到FlatMapSequention有一个队列的缓冲区大小,但是我不明白为什么ConcatMap不需要一个。

I've read from the documentation that flatMap:

Transform the elements emitted by this Flux asynchronously into Publishers, then flatten these inner publishers into a single Flux through merging, which allow them to interleave.

that flatMapSequential:

Transform the elements emitted by this Flux asynchronously into Publishers, then flatten these inner publishers into a single Flux, but merge them in the order of their source element.

and that concatMap:

Transform the elements emitted by this Flux asynchronously into Publishers, then flatten these inner publishers into a single Flux, sequentially and preserving order using concatenation.
There are three dimensions to this operator that can be compared with flatMap and flatMapSequential:

Generation of inners and subscription: this operator waits for one inner to complete before generating the next one and subscribing to it.

Ordering of the flattened values: this operator naturally preserves the same order as the source elements, concatenating the inners from each source element sequentially.

Interleaving: this operator does not let values from different inners interleave (concatenation).

The difference between flatMap and the other two is pretty understandable, but I don't understand when the difference between concatMap and flatMapSequential takes place. Is there any performance difference between the two? I've read that flatMapSequential has a buffer size for some queue, but I don't understand why concatMap doesn't need one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2025-01-30 10:21:39

' flatmap 和flatmapsequepential 运营商急切地订阅concatmap 等待每个内部完成对此。

让我们看看一个示例:

  @Test
  void test_flatMap() {
    Flux.just(1, 2, 3)
        .flatMap(this::doSomethingAsync)
        //.flatMapSequential(this::doSomethingAsync)
        //.concatMap(this::doSomethingAsync)
        .doOnNext(n -> log.info("Done {}", n))
        .blockLast();
  }

  private Mono<Integer> doSomethingAsync(Integer number) {
    //add some delay for the second item...
    return number == 2 ? Mono.just(number).doOnNext(n -> log.info("Executing {}", n)).delayElement(Duration.ofSeconds(1))
        : Mono.just(number).doOnNext(n -> log.info("Executing {}", n));
  }

输出:

2022-04-22 19:38:49,164  INFO main - Executing 1
2022-04-22 19:38:49,168  INFO main - Done 1
2022-04-22 19:38:49,198  INFO main - Executing 2
2022-04-22 19:38:49,200  INFO main - Executing 3
2022-04-22 19:38:49,200  INFO main - Done 3
2022-04-22 19:38:50,200  INFO parallel-1 - Done 2

如您所见,flatmap不能保留原始订购,并且急切地订阅了所有三个元素。另外,请注意,元素3已在元素2之前进行。

这是使用FlatmapSequention的输出:

2022-04-22 19:53:40,229  INFO main - Executing 1
2022-04-22 19:53:40,232  INFO main - Done 1
2022-04-22 19:53:40,261  INFO main - Executing 2
2022-04-22 19:53:40,263  INFO main - Executing 3
2022-04-22 19:53:41,263  INFO parallel-1 - Done 2
2022-04-22 19:53:41,264  INFO parallel-1 - Done 3

Flatmapsequential已急切地订阅了所有三个元素,例如flatsmap但保留了按订单收到的排队元素的订单。

这是使用ConcatMap的输出:

2022-04-22 19:59:31,817  INFO main - Executing 1
2022-04-22 19:59:31,820  INFO main - Done 1
2022-04-22 19:59:31,853  INFO main - Executing 2
2022-04-22 19:59:32,857  INFO parallel-1 - Done 2
2022-04-22 19:59:32,857  INFO parallel-1 - Executing 3
2022-04-22 19:59:32,857  INFO parallel-1 - Done 3

ConcatMap&nbsp;自然地保留与源元素相同的顺序。

The flatMap and flatMapSequential operators subscribe eagerly, the concatMap waits for each inner completion before generating the next sub-stream and subscribing to it.

Let's see an example:

  @Test
  void test_flatMap() {
    Flux.just(1, 2, 3)
        .flatMap(this::doSomethingAsync)
        //.flatMapSequential(this::doSomethingAsync)
        //.concatMap(this::doSomethingAsync)
        .doOnNext(n -> log.info("Done {}", n))
        .blockLast();
  }

  private Mono<Integer> doSomethingAsync(Integer number) {
    //add some delay for the second item...
    return number == 2 ? Mono.just(number).doOnNext(n -> log.info("Executing {}", n)).delayElement(Duration.ofSeconds(1))
        : Mono.just(number).doOnNext(n -> log.info("Executing {}", n));
  }

Output:

2022-04-22 19:38:49,164  INFO main - Executing 1
2022-04-22 19:38:49,168  INFO main - Done 1
2022-04-22 19:38:49,198  INFO main - Executing 2
2022-04-22 19:38:49,200  INFO main - Executing 3
2022-04-22 19:38:49,200  INFO main - Done 3
2022-04-22 19:38:50,200  INFO parallel-1 - Done 2

As you can see, flatMap does not preserve original ordering, and has subscribed to all three elements eagerly. Also, notice that element 3 has proceeded before element 2.

Here is the output using flatMapSequential:

2022-04-22 19:53:40,229  INFO main - Executing 1
2022-04-22 19:53:40,232  INFO main - Done 1
2022-04-22 19:53:40,261  INFO main - Executing 2
2022-04-22 19:53:40,263  INFO main - Executing 3
2022-04-22 19:53:41,263  INFO parallel-1 - Done 2
2022-04-22 19:53:41,264  INFO parallel-1 - Done 3

flatMapSequential has subscribed to all three elements eagerly like flatMap but preserves the order by queuing elements received out of order.

Here is the output using concatMap:

2022-04-22 19:59:31,817  INFO main - Executing 1
2022-04-22 19:59:31,820  INFO main - Done 1
2022-04-22 19:59:31,853  INFO main - Executing 2
2022-04-22 19:59:32,857  INFO parallel-1 - Done 2
2022-04-22 19:59:32,857  INFO parallel-1 - Executing 3
2022-04-22 19:59:32,857  INFO parallel-1 - Done 3

concatMap naturally preserves the same order as the source elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文