RXJAVA-concatMap
与flatMap类似,concatMap它和flatMap的作用几乎一模一样, 只是它的结果是严格按照上游发送的顺序来发送的, 来看个代码吧:
package com.netty.demo.vertx;
import io.reactivex.*;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
public class RxJavaTest {
public static void main(String[] args) throws InterruptedException {
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
}
}).observeOn(Schedulers.newThread()).subscribeOn(Schedulers.io())
.concatMap(new Function>() {
@Override
public ObservableSource apply(Integer integer) throws Exception {
final List list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("I am value " + integer);
}
log.info(Arrays.toString(list.toArray()));
return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
log.info(s);
}
});
Thread.sleep(300000000);
}
}
2021-02-22 15:46:02 [RxNewThreadScheduler-1] INFO c.n.d.v.RxJavaTest:apply - [I am value 1, I am value 1, I am value 1] 2021-02-22 15:46:02 [RxComputationThreadPool-1] INFO c.n.d.v.RxJavaTest:accept - I am value 1 2021-02-22 15:46:02 [RxComputationThreadPool-1] INFO c.n.d.v.RxJavaTest:accept - I am value 1 2021-02-22 15:46:02 [RxComputationThreadPool-1] INFO c.n.d.v.RxJavaTest:accept - I am value 1 2021-02-22 15:46:02 [RxComputationThreadPool-1] INFO c.n.d.v.RxJavaTest:apply - [I am value 2, I am value 2, I am value 2] 2021-02-22 15:46:02 [RxComputationThreadPool-2] INFO c.n.d.v.RxJavaTest:accept - I am value 2 2021-02-22 15:46:02 [RxComputationThreadPool-2] INFO c.n.d.v.RxJavaTest:accept - I am value 2 2021-02-22 15:46:02 [RxComputationThreadPool-2] INFO c.n.d.v.RxJavaTest:accept - I am value 2 2021-02-22 15:46:02 [RxComputationThreadPool-2] INFO c.n.d.v.RxJavaTest:apply - [I am value 3, I am value 3, I am value 3] 2021-02-22 15:46:02 [RxComputationThreadPool-3] INFO c.n.d.v.RxJavaTest:accept - I am value 3 2021-02-22 15:46:02 [RxComputationThreadPool-3] INFO c.n.d.v.RxJavaTest:accept - I am value 3 2021-02-22 15:46:02 [RxComputationThreadPool-3] INFO c.n.d.v.RxJavaTest:accept - I am value 3
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~