Rxjava(线程类)--observeOn和subscribeOn实例

时间:2022-04-30 17:46:04

observeOn作用于该操作符之后操作符直到出现新的observeOn操作符

        System.out.println("<<<<<< main threadid = " + Thread.currentThread().getId());
Observable.just(1).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer integer) {
System.out.println("<<<<<< thread id = " + Thread.currentThread().getId());
return integer * 2;
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).flatMap(new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(Integer integer) {
System.out.println("<<<<<< thread id = " + Thread.currentThread().getId());
return Observable.just(integer);
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.println("<<<<<< thread id = " + Thread.currentThread().getId());
System.out.print(integer + ", ");
}
});

结果:

<<<<<< main threadid = 1
<<<<<< thread id = 1
<<<<<< thread id = 15
<<<<<< thread id = 13
第一个map之后我们没改变线程,使用的是前面一样的进程,而后面我们利用 observeOn改变了线程,打印的id也不一样


subscribeOn 作用于该操作符之前的 Observable 的创建操符作以及 doOnSubscribe 操作符 ,换句话说就是 doOnSubscribe 以及 Observable 的创建操作符总是被其之后最近的 subscribeOn 控制。

我们看几个例子就明白了

 System.out.println("<<<<<< main threadid = " + Thread.currentThread().getId());
Observable.just(1).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer integer) {
System.out.println("<<<<<<map thread id = " + Thread.currentThread().getId());
return integer * 2;
}
}).doOnSubscribe(new Action0() {
@Override
public void call() {
System.out.println("<<<<<<doOnSubscribe thread id = " + Thread.currentThread().getId());
}
}).subscribeOn(Schedulers.from(JobExecutor.getInstance())).observeOn(Schedulers.from(JobExecutor.getInstance())).flatMap(new Func1<Integer,
Observable<Integer>>
() {
@Override
public Observable<Integer> call(Integer integer) {
System.out.println("<<<<<<flatMap thread id = " + Thread.currentThread().getId());
return Observable.just(integer);
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.println("<<<<<<subscribe thread id = " + Thread.currentThread().getId());
System.out.print(integer + ", ");
}
});
输出;

<<<<<< main threadid = 1
<<<<<<doOnSubscribe thread id = 15
<<<<<<map thread id = 15
<<<<<<flatMap thread id = 13
<<<<<<subscribe thread id = 14

doOnSubscribe() 执行在 subscribe() 发生的线程, 会在 subscribe 刚开始,而事件还未发送之前被调用,可以用于做一些准备工作。这里我们可以看到doOnSubscribe后面有一个subscribeOn,doOnSubscribe执行的线程也会被其影响,我们再看一下在之前的情况。

 System.out.println("<<<<<< main threadid = " + Thread.currentThread().getId());
Observable.just(1).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer integer) {
System.out.println("<<<<<<map thread id = " + Thread.currentThread().getId());
return integer * 2;
}
}).subscribeOn(Schedulers.from(JobExecutor.getInstance())).doOnSubscribe(new Action0() {
@Override
public void call() {
System.out.println("<<<<<<doOnSubscribe thread id = " + Thread.currentThread().getId());
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).flatMap(new Func1<Integer,
Observable<Integer>>
() {
@Override
public Observable<Integer> call(Integer integer) {
System.out.println("<<<<<<flatMap thread id = " + Thread.currentThread().getId());
return Observable.just(integer);
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.println("<<<<<<subscribe thread id = " + Thread.currentThread().getId());
System.out.print(integer + ", ");
}
});
<<<<<< main threadid = 1<<<<<<doOnSubscribe thread id = 1<<<<<<map thread id = 15<<<<<<flatMap thread id = 14<<<<<<subscribe thread id = 13

可以看到 doOnSubscribe执行线程没有收到影响,因为前面没有subscribeOn


再看一下有多个subscribeOn

      System.out.println("<<<<<< main threadid = " + Thread.currentThread().getId());
Observable.just(1).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer integer) {
System.out.println("<<<<<<map thread id = " + Thread.currentThread().getId());
return integer * 2;
}
}).doOnSubscribe(new Action0() {
@Override
public void call() {
System.out.println("<<<<<<doOnSubscribe1 thread id = " + Thread.currentThread().getId());
}
}).subscribeOn(Schedulers.from(JobExecutor.getInstance())).doOnSubscribe(new Action0() {
@Override
public void call() {
System.out.println("<<<<<<doOnSubscribe2 thread id = " + Thread.currentThread().getId());
}
}).subscribeOn(Schedulers.from(JobExecutor.getInstance())).observeOn(Schedulers.from(JobExecutor.getInstance
())).flatMap(new Func1<Integer,
Observable<Integer>>
() {
@Override
public Observable<Integer> call(Integer integer) {
System.out.println("<<<<<<flatMap thread id = " + Thread.currentThread().getId());
return Observable.just(integer);
}
}).observeOn(Schedulers.from(JobExecutor.getInstance())).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.println("<<<<<<subscribe thread id = " + Thread.currentThread().getId());
System.out.print(integer + ", ");
}
});
<<<<<< main threadid = 1<<<<<<doOnSubscribe2 thread id = 15<<<<<<doOnSubscribe1 thread id = 13<<<<<<map thread id = 14<<<<<<flatMap thread id = 13<<<<<<subscribe thread id = 15

可以看到, subscribeOn影响的是它之前的,而且多个也有效。

参考:

http://www.jianshu.com/p/59c3d6bb6a6b