通过多个实例来对比分析
注:test4()运行在主线程,所以下文中的 主线程均为指 test4()所在的线程。当然,可以将test4()运行在非主线程
- 1、不使用subscribeOn 和 observeOn
public class RxJavaDemoActivity extends BasicActivity{
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rxjavamain);
test4();
}
private void test4() {
Log.i("test0",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
Log.i("test1",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
subscriber.onNext(R.drawable.picasso_drawable);
}
})
.map(new Func1<Integer, Drawable>() {
@Override
public Drawable call(Integer integer) {
Log.i("test2",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
Drawable drawable = getResources().getDrawable(integer);
return drawable;
}
})
.subscribe(new Subscriber<Drawable>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(Drawable drawable) {
Log.i("test3",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
}
});
}
}
输出结果如下:
08-17 09:06:14.028 8581-8581/net.yuanjin I/test0: main ,id = 1
08-17 09:06:14.028 8581-8581/net.yuanjin I/test1: main ,id = 1
08-17 09:06:14.028 8581-8581/net.yuanjin I/test2: main ,id = 1
08-17 09:06:14.036 8581-8581/net.yuanjin I/test3: main ,id = 1
可以看到,不使用 subscribeOn 和 observeOn 的时候,全部操作都是在主线程中执行
- 2、仅使用 subscribeOn
/**
* 仅使用 subscribeOn(Schedulers.newThread())
*/
private void test4() {
Log.i("test0",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
Log.i("test1",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
subscriber.onNext(R.drawable.picasso_drawable);
}
})
.map(new Func1<Integer, Drawable>() {
@Override
public Drawable call(Integer integer) {
Log.i("test2",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
return getResources().getDrawable(integer);
}
})
.subscribeOn(Schedulers.newThread())
.subscribe(new Subscriber<Drawable>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(Drawable drawable) {
Log.i("test3",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
}
});
}
输出结果:
08-17 09:13:02.052 15056-15056/net.yuanjin I/test0: main ,id = 1
08-17 09:13:02.052 15056-15402/net.yuanjin I/test1: RxNewThreadScheduler-2 ,id = 272
08-17 09:13:02.052 15056-15402/net.yuanjin I/test2: RxNewThreadScheduler-2 ,id = 272
08-17 09:13:02.056 15056-15402/net.yuanjin I/test3: RxNewThreadScheduler-2 ,id = 272
可以看到,当使用了 subscribeOn(Schedulers.newThread())
后,从test1 - test3全都在新的同一线程中执行。所以 subscribeOn 影响了 从触发事件(test1)开始及其之后的所有事件的线程(如果后面事件的线程未重定义的话)
- 3、仅使用 observeOn
/**
* 仅使用 observeOn(Schedulers.newThread())
*/
private void test4() {
Log.i("test0",Thread.currentThread().getName().toString() + " ,id = "+Thread.currentThread().getId());
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
Log.i("test1",Thread.currentThread().getName().toString() +