RxJava2 Flowable的concatMap和concatMapEager与observeOn的行为不同

时间:2019-03-28 02:05:04

标签: rx-java rx-java2 concatmap

当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吗?还是故意的。

0 个答案:

没有答案