Java开发过程中都会碰到异步任务
,通常通过回调的方式来实现。但是业务一旦复杂,会使回调的书写很麻烦。(所谓回调地狱
)
RxJava的链式实现,可以将异步业务以同步的方式书写。
RxJava中的线程调度(控制)操作符为subscribeOn
与 observeOn
。
subscribeOn
[图片上传失败...(image-ee9ce7-1519354360186)]
public final Observable subscribeOn(Scheduler scheduler) {
return subscribeOn(scheduler, !(this.onSubscribe instanceof OnSubscribeCreate));
}
public final Observable subscribeOn(Scheduler scheduler, boolean requestOn) {
if (this instanceof ScalarSynchronousObservable) {
return ((ScalarSynchronousObservable)this).scalarScheduleOn(scheduler);
}
return unsafeCreate(new OperatorSubscribeOn(this, scheduler, requestOn));
}
OperatorSubscribeOn
public final class OperatorSubscribeOn implements OnSubscribe {
final Scheduler scheduler;
final Observable source;
final boolean requestOn;
public OperatorSubscribeOn(Observable source, Scheduler scheduler, boolean requestOn) {
this.scheduler = scheduler;
this.source = source;
this.requestOn = requestOn;
}
@Override
public void call(final Subscriber super T> subscriber) {
final Worker inner = scheduler.createWorker();
SubscribeOnSubscriber parent = new SubscribeOnSubscriber(subscriber, requestOn, inner, source);
subscriber.add(parent);
subscriber.add(inner);
inner.schedule(parent);
}
static final class SubscribeOnSubscriber extends Subscriber implements Action0 {
final Subscriber super T> actual;
final boolean requestOn;
final Worker worker;
Observable source;
Thread t;
SubscribeOnSubscriber(Subscriber super T> actual, boolean requestOn, Worker worker, Observable source) {
this.actual = actual;
this.requestOn = requestOn;
this.worker = worker;
this.source = source;
}
@Override
public void onNext(T t) {
actual.onNext(t);
}
@Override
public void onError(Throwable e) {
try {
actual.onError(e);
} finally {
worker.unsubscribe();
}
}
@Override
public void onCompleted() {
try {
actual.onCompleted();
} finally {
worker.unsubscribe();
}
}
@Override
public void call() {
Observable src = source;
source = null;
t = Thread.currentThread();
src.unsafeSubscribe(this);
}
}
即实际可以理解成:
Observable.just("goods")
.subscribeOn(Schedulers.io())
.subscribe();
等价于:
Schedulers.io().createWorker().schedule(new Action0() {
@Override
public void call() {
Observable.just("goods").subscribe();
}
});
observeOn
public final Observable observeOn(Scheduler scheduler) {
return observeOn(scheduler, RxRingBuffer.SIZE);
}
public final Observable observeOn(Scheduler scheduler, int bufferSize) {
return observeOn(scheduler, false, bufferSize);
}
public final Observable observeOn(Scheduler scheduler, boolean delayError, int bufferSize) {
if (this instanceof ScalarSynchronousObservable) {
return ((ScalarSynchronousObservable)this).scalarScheduleOn(scheduler);
}
return lift(new OperatorObserveOn(scheduler, delayError, bufferSize));
}
OperatorObserveOn
public final class OperatorObserveOn implements Operator {
private final Scheduler scheduler;
private final boolean delayError;
private final int bufferSize;
public OperatorObserveOn(Scheduler scheduler, boolean delayError) {
this(scheduler, delayError, RxRingBuffer.SIZE);
}
@Override
public Subscriber super T> call(Subscriber super T> child) {
if (scheduler instanceof ImmediateScheduler) {
// avoid overhead, execute directly
return child;
} else if (scheduler instanceof TrampolineScheduler) {
// avoid overhead, execute directly
return child;
} else {
ObserveOnSubscriber parent = new ObserveOnSubscriber(scheduler, child, delayError, bufferSize);
parent.init();
return parent;
}
}
public static Operator rebatch(final int n) {
return new Operator() {
@Override
public Subscriber super T> call(Subscriber super T> child) {
ObserveOnSubscriber parent = new ObserveOnSubscriber(Schedulers.immediate(), child, false, n);
parent.init();
return parent;
}
};
}
/** Observe through individual queue per observer. */
static final class ObserveOnSubscriber extends Subscriber implements Action0 {
final Subscriber super T> child;
final Scheduler.Worker recursiveScheduler;
final boolean delayError;
final Queue
即实际可以理解成:
Observable.just("goods")
.observeOn(Schedulers.io())
.subscribe(new Action1() {
@Override
public void call(String s) {
}
});
等价于:
Observable.just("goods")
.subscribe(new Action1() {
@Override
public void call(String s) {
Schedulers.io().createWorker().schedule(new Action0() {
@Override
public void call() {
}
});
}
});