Flux<Long> flux1 = Flux
.<Long>create(fluxSink -> {
for (long i = 0; i < 20; i++) {
fluxSink.next(i);
}
})
.filter(aLong -> aLong % 2 == 0)
.doOnNext(aLong -> System.out.println("flux 1 : " + aLong));
Flux<Long> flux2 = Flux
.<Long>create(fluxSink -> {
for (long i = 0; i < 20; i++) {
fluxSink.next(i);
}
})
.filter(aLong -> aLong % 2 == 1)
.doOnNext(aLong -> System.out.println("flux 2 : " + aLong));
Flux.merge(flux1, flux2)
.doOnNext(System.out::println)
.then()
.block();
创建两个Flux<Long>
类似的上层代码。
flux1创建偶数流(0,2,4,6,8 ...) flux2创建奇数流(1、3、5、7、9 ...)
我希望将这两个flux1和flux2合并时的工作方式
0,1,2,3,4 ...
或0,2,1,3,4..
取决于计算能力
但始终要花费助焊剂1和花费助焊剂2 (flux1 start)0,2,4,6,8, ... 16,18,(flux1 end)(flux2 start)1,3,5,7 ... 17,19
如何快速预订多个流量事件?
答案 0 :(得分:1)
两个流都在同一线程上运行。订阅后,flux1
开始推送数据,直到完成为止。只有这样线程才能释放,flux2
才能继续。 merge
运算符按到达顺序发射值。它不会在第一流和第二流之间切换。
如果要同时运行流,则需要在不同的线程上运行它们,例如通过使用publishOn
运算符。
Flux<Long> flux1 = Flux
.<Long>create(fluxSink -> {
for (long i = 0; i < 20; i++) {
fluxSink.next(i);
}
})
.publishOn(Schedulers.newSingle("thread-x")
.filter(aLong -> aLong % 2 == 0)
.doOnNext(aLong -> System.out.println("flux 1 : " + aLong));