[英]Reactor - Async / non-blocking
I thought below flux chain will be placed / executed via event loop (like JS).我认为下面的通量链将通过事件循环(如 JS)放置/执行。 So running the below code will print the blocking
for
loop first & then flux chain will be executed.因此,运行以下代码将首先打印阻塞
for
循环,然后执行通量链。
But the entire flux is executed first always before it moves to for
loop.但是整个通量总是在它移动到
for
循环之前首先执行。 [I do have some sleep
statements which is blocking. [我确实有一些阻塞的
sleep
语句。 But there are in 2 doOnNext
stages]但是有 2 个
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));
}
It prints它打印
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
Could someone please explain this behavior and answer these questions?有人可以解释这种行为并回答这些问题吗?
publishOn
/ subscribeOn
then the output should be:publishOn
/ subscribeOn
然后 output 应该是: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
For more about publishOn
vs subscribeOn
see: link有关
publishOn
与subscribeOn
的更多信息,请参见: 链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.