反应堆上的通量操作会按顺序发生吗?

发布于 2025-01-14 13:58:48 字数 145 浏览 3 评论 0原文

如果我在通量上有两个平面图,它们将始终按顺序执行

Flux.just(1,2,3,4)
    .flatMap(...) 
    .flatMap(...) // Will this always execute 2nd?

If I have two flatmaps on a flux will the they always execute in sequence

Flux.just(1,2,3,4)
    .flatMap(...) 
    .flatMap(...) // Will this always execute 2nd?

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

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

发布评论

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

评论(2

薆情海 2025-01-21 13:58:48

这取决于你到底想表达什么——具体细节在这里很重要。

对于任何给定元素,答案是肯定的 - 它将由第一个 flatMap 调用处理,然后由第二个 flatMap 调用处理该 flatMap 调用的结果。回想一下,reactor 中的操作符与普通的 Java 流类似,是链式的——因此没有明智的方法可以让它们不按顺序操作。

但对于所有元素来说,不会 - 因为它不会处理所有 4 个元素的第一个 flatMap 调用,然后处理所有 4 个元素的第二个 flatMap 调用 - 每个元素都是独立处理的。当您认为 Flux 类似于可能没有尽头的元素流,而不是定义的有界集合时,如果它不这样做就没有多大意义。

It depends on what exactly you mean - specifics are important here.

For any given element the answer is yes - it will be processed by the first flatMap call, then the results of that flatMap call processed by the second flatMap call. Recall that operators in reactor, similar to vanilla Java streams, are chained - so there's no sensible way they could ever operate out of sequence.

For all elements in total though, no - as in it won't process the first flatMap call for all 4 elements, then process the second flatMap call for all 4 elements - each element is treated independently. When you consider that a Flux is analogous to a stream of elements that may have no end, rather than a defined, bounded collection, it wouldn't make much sense if it didn't behave this way.

讽刺将军 2025-01-21 13:58:48

让我们考虑这些小例子:

Flux.just(3, 2, 1)
  .flatMap(e -> {
    System.out.println("First FlatMap : " + e);
    return Mono.just(e).delayElement(Duration.ofMillis(e * 100));
  })
  .flatMap(e -> {
    System.out.println("Second FlatMap : " + e);
    return Mono.just(e);
  })
  .blockLast();

/*
First FlatMap : 3
First FlatMap : 2
First FlatMap : 1
Second FlatMap : 1
Second FlatMap : 2
Second FlatMap : 3
*/
Flux.just(3, 2, 1)
  .flatMap(e -> {
    System.out.println("First FlatMap : " + e);
    return Mono.just(e);
  })
  .flatMap(e -> {
    System.out.println("Second FlatMap : " + e);
    return Mono.just(e);
  })
  .blockLast();

/*
First FlatMap : 3
Second FlatMap : 3
First FlatMap : 2
Second FlatMap : 2
First FlatMap : 1
Second FlatMap : 1
*/

我们可以说操作员按照元素进入的顺序处理元素。

Let's consider these small examples :

Flux.just(3, 2, 1)
  .flatMap(e -> {
    System.out.println("First FlatMap : " + e);
    return Mono.just(e).delayElement(Duration.ofMillis(e * 100));
  })
  .flatMap(e -> {
    System.out.println("Second FlatMap : " + e);
    return Mono.just(e);
  })
  .blockLast();

/*
First FlatMap : 3
First FlatMap : 2
First FlatMap : 1
Second FlatMap : 1
Second FlatMap : 2
Second FlatMap : 3
*/
Flux.just(3, 2, 1)
  .flatMap(e -> {
    System.out.println("First FlatMap : " + e);
    return Mono.just(e);
  })
  .flatMap(e -> {
    System.out.println("Second FlatMap : " + e);
    return Mono.just(e);
  })
  .blockLast();

/*
First FlatMap : 3
Second FlatMap : 3
First FlatMap : 2
Second FlatMap : 2
First FlatMap : 1
Second FlatMap : 1
*/

We can say an operator process the elements in the order they came in.

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