[英]Reactor - Async / non-blocking
我認為下面的通量鏈將通過事件循環(如 JS)放置/執行。 因此,運行以下代碼將首先打印阻塞for
循環,然后執行通量鏈。
但是整個通量總是在它移動到for
循環之前首先執行。 [我確實有一些阻塞的sleep
語句。 但是有 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));
}
它打印
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
有人可以解釋這種行為並回答這些問題嗎?
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
有關publishOn
與subscribeOn
的更多信息,請參見: 鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.