当concatMapEager将并发与1一起使用时,publisher.observerOn(scheduler).concatMap(mapper, 1)
和publisher.observerOn(scheduler).concatMapEager(mapper, 1, 1)
应该具有相同的行为吗?
我测试了concatMap
,它可能使用了不同的线程而不是observeOn
中的调度程序来调用映射器。
例如
Flowable.just(1, 2, 3, 4, 5)
.observeOn(Schedulers.io())
.concatMapEager(i -> {
System.out.println("1>>>>trigger in thread:" + Thread.currentThread());
return Flowable.create(emitter -> {
executorService.submit(() -> {
try {
Thread.sleep(3000L);
} catch (InterruptedException ignore) {
}
emitter.onNext("" + i);
emitter.onComplete();
});
}, BackpressureStrategy.DROP);
}, 1, 1)
.map(i -> "ok:" + i)
.blockingSubscribe();
此打印
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
但是如果将其更改为concatMap(mapper, 1)
,它将打印
1>>>>trigger in thread:Thread[RxCachedThreadScheduler-1,5,main]
1>>>>trigger in thread:Thread[pool-1-thread-1,5,main]
1>>>>trigger in thread:Thread[pool-1-thread-2,5,main]
1>>>>trigger in thread:Thread[pool-1-thread-3,5,main]
1>>>>trigger in thread:Thread[pool-1-thread-4,5,main]
concatMap
也应该使用RxCachedThreadScheduler
吗?还是故意的。