我认为下面的磁链将通过事件循环(如JS)放置/执行。因此,运行以下代码将首先打印阻塞的for
循环,然后将执行磁链。
但是总要在移入for
循环之前首先执行整个磁通。 [我确实有一些sleep
语句正在阻止。但是有两个doOnNext
阶段]
AtomicInteger atomicInteger = new AtomicInteger(0);
// reactor
Flux.generate(synchronousSink -> {
if (atomicInteger.incrementAndGet() < 3) {
synchronousSink.next(atomicInteger.get());
} else
synchronousSink.complete();
})
.doOnNext(i -> {
System.out.println(
"A - Received " + i + " by " + Thread.currentThread().getName()
);
sleep(Duration.ofSeconds(1));
}).doOnNext(i -> {
System.out.println(
"B - Received " + i + " : by " + Thread.currentThread().getName()
);
sleep(Duration.ofSeconds(1));
}).subscribe();
for (int i = 0; i < 5; i++) {
System.out.println("For " + i + " by " + Thread.currentThread().getName());
sleep(Duration.ofMillis(500));
}
它打印
A - Received 1 by main
B - Received 1 by main
A - Received 2 by main
B - Received 2 by main
For 0 by main
For 1 by main
For 2 by main
For 3 by main
For 4 by main
有人可以解释这种行为并回答这些问题吗?
答案 0 :(得分:0)
publishOn
/ subscribeOn
,然后输出应为:For 0 by main A - Received 1 by boundedElastic-3 For 1 by main For 2 by main B - Received 1 : by boundedElastic-3 For 3 by main For 4 by main A - Received 2 by boundedElastic-3
有关publishOn
与subscribeOn
的更多信息,请参见:link