Rxjava在Android界很火,不过博主工作并不接触网络开发,没有大批量多层次的使用异步任务的机会。一直听说但是并没有使用,更不会深入分析它的机制。不过出于好奇本文会研究下源码中个人关注的点,注意本文并不介绍rxjava如何使用。
源码版本是RxJava-2.x,2018.1.5下载。
rxjava的起点就是Observable.java
先看下create的demo代码:
Observable observable = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
emitter.onNext(1);
emitter.onComplete();
}
});
create源码如下:
public static Observable create(ObservableOnSubscribe source) {
ObjectHelper.requireNonNull(source, "source is null");
return RxJavaPlugins.onAssembly(new ObservableCreate(source));
}
create是创建Observable对象的方法之一,该类中还有其他诸多的创建方法,如from开头的一堆方法。只以该方法了解下Observable的创建流程。
public static T requireNonNull(T object, String message) {
if (object == null) {
throw new NullPointerException(message);
}
return object;
}
public static Observable onAssembly(@NonNull Observable source) {
Function super Observable, ? extends Observable> f = onObservableAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
requireNonNull和OnAssembly是使用频率很高的方法,但是这两个方法只是起辅助的作用,对理解整个流程并无实质作用。
requireNonNull是判断参数是否为null,null的话会抛出空指针异常;OnAssembly是hook机制,如果onObservableAssembly方法不为空的话,使用该方法做些额外操作,默认onObservableAssembly是null。那么create方法实际上可以等同于下:
public static Observable create(ObservableOnSubscribe source) {
return new ObservableCreate(source);
}
参数source的类型是ObservableOnSubscribe,是个接口,其中的subscribe要用户实现。
public interface ObservableOnSubscribe {
/**
* Called for each Observer that subscribes.
* @param emitter the safe emitter instance, never null
* @throws Exception on error
*/
void subscribe(@NonNull ObservableEmitter emitter) throws Exception;
}
接口subscribe的参数是ObservableEmitter,该类主要的作用是可以让Observable的操作有取消的功能。
public interface ObservableEmitter extends Emitter {
/**
* Sets a Disposable on this emitter; any previous Disposable
* or Cancellation will be unsubscribed/cancelled.
* @param d the disposable, null is allowed
*/
void setDisposable(@Nullable Disposable d);
/**
* Sets a Cancellable on this emitter; any previous Disposable
* or Cancellation will be unsubscribed/cancelled.
* @param c the cancellable resource, null is allowed
*/
void setCancellable(@Nullable Cancellable c);
...
}
基类Emitter,这里可以看到和观察者一模一样的接口,例如OnNext,后续会看到Emitter的接口实现终将会调用观察者实现的同名接口
public interface Emitter {
/**
* Signal a normal value.
* @param value the value to signal, not null
*/
void onNext(@NonNull T value);
/**
* Signal a Throwable exception.
* @param error the Throwable to signal, not null
*/
void onError(@NonNull Throwable error);
/**
* Signal a completion.
*/
void onComplete();
}
public final class ObservableCreate extends Observable {
final ObservableOnSubscribe source; //这里就是用户实现的接口,作为参数传入
public ObservableCreate(ObservableOnSubscribe source) {
this.source = source;
}
...
}
这个类如果不看subscribe流程相关方法的话十分简单,就是保存所传入的参数为source。
承接上个demo,建立连接的demo代码如下
Observer observer = new Observer() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "subscribe");
}
@Override
public void onNext(Integer value) {
Log.d(TAG, "" + value);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "error");
}
@Override
public void onComplete() {
Log.d(TAG, "complete");
}
};
//建立连接
observable.subscribe(observer);
subscribe源码:
public final void subscribe(Observer super T> observer) {
...
subscribeActual(observer);
...
}
observable的实际类型是ObservableCreate
protected void subscribeActual(Observer super T> observer) {
CreateEmitter parent = new CreateEmitter(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
CreateEmitter(Observer super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
...
if (!isDisposed()) {
observer.onNext(t);
}
}
...
}
subscribeActual是subcribe订阅流程的核心方法,创建了内部类CreateEmitter对象,CreateEmitter创建时传入的参数就是Observer,这里就可以看出onNext最终是调用了Observer的同名方法。这里Observable和Observer建立起了连接,这个框架比较巧妙。
这个就是rxjava的基本流程,建立连接就是Observable回调Observer接口的过程。当然demo是没什么卵用的,不使用Observable提供的大量Operator是看不出Rxjava的过人之处的。
先看段线程控制的demo代码
Observable.create(...)
.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
.subscribe(...);
public final Observable subscribeOn(Scheduler scheduler) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableSubscribeOn(this, scheduler));
}
这个方法和create似曾相识,结构一模一样,就是传入的参数和返回的结果不同而已
public final class ObservableSubscribeOn extends AbstractObservableWithUpstream {
final Scheduler scheduler;
public ObservableSubscribeOn(ObservableSource source, Scheduler scheduler) {
super(source);
this.scheduler = scheduler;
}
@Override
public void subscribeActual(final Observer super T> s) {
final SubscribeOnObserver parent = new SubscribeOnObserver(s);
s.onSubscribe(parent);
parent.setDisposable(scheduler.scheduleDirect(new SubscribeTask(parent)));
}
static final class SubscribeOnObserver extends AtomicReference implements Observer, Disposable {
private static final long serialVersionUID = 8094547886072529208L;
final Observer super T> actual;
final AtomicReference s;
SubscribeOnObserver(Observer super T> actual) {
this.actual = actual;
this.s = new AtomicReference();
}
@Override
public void onSubscribe(Disposable s) {
DisposableHelper.setOnce(this.s, s);
}
@Override
public void onNext(T t) {
actual.onNext(t);
}
...
}
final class SubscribeTask implements Runnable {
private final SubscribeOnObserver parent;
SubscribeTask(SubscribeOnObserver parent) {
this.parent = parent;
}
@Override
public void run() {
source.subscribe(parent);
//注意这里的source实际类型在demo中是ObservableCreate,然后最终会走到ObservableCreate的subscribeActual中。当然这个source可以是其他的任意Observer对象
}
}
}
看下基类
abstract class AbstractObservableWithUpstream extends Observable implements HasUpstreamObservableSource
果然也是继承自Observalbe。ObservableSubscribeOn和ObservableCreate的架构一模一样有没有。保存了传入的参数,实现了subscribeActual方法。subscribeActual的核心是实例化了一个内部类,并将其作为参数传入了另一个方法。
public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
final Worker w = createWorker();
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
DisposeTask task = new DisposeTask(decoratedRun, w);
w.schedule(task, delay, unit);
return task;
}
Worker是创建工作线程的,而run参数是个Runnable,就是SubscribeTask实例。scheduleDirect就是将SubscribeTask运行在了Worker的指定线程,Worker具体怎么工作不具体分析了,其实最终就是使用java标准的线程池,和Android的AsyncTask一样。而SubscribeTask的run中只有一行
source.subscribe(parent);
此时该行代码已经运行在另一个线程中了。
public final Observable observeOn(Scheduler scheduler, boolean delayError, int bufferSize) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
ObjectHelper.verifyPositive(bufferSize, "bufferSize");
return RxJavaPlugins.onAssembly(new ObservableObserveOn(this, scheduler, delayError, bufferSize));
}
还是似曾相识的代码
public final class ObservableObserveOn extends AbstractObservableWithUpstream
protected void subscribeActual(Observer super T> observer) {
if (scheduler instanceof TrampolineScheduler) {
source.subscribe(observer);
} else {
Scheduler.Worker w = scheduler.createWorker();
source.subscribe(new ObserveOnObserver(observer, w, delayError, bufferSize));
}
}
static final class ObserveOnObserver extends BasicIntQueueDisposable
implements Observer, Runnable {
...
final Scheduler.Worker worker;
...
SimpleQueue queue; //使用队列存储参数
...
ObserveOnObserver(Observer super T> actual, Scheduler.Worker worker, boolean delayError, int bufferSize) {
this.actual = actual;
this.worker = worker;
this.delayError = delayError;
this.bufferSize = bufferSize;
}
...
@Override
public void onNext(T t) {
...
schedule(); //并没有直接调用actual的onNext方法
}
...
void schedule() {
if (getAndIncrement() == 0) {
worker.schedule(this); //调度后续代码到线程中
}
}
void drainNormal() {
int missed = 1;
final SimpleQueue q = queue;
final Observer super T> a = actual;
for (;;) {
if (checkTerminated(done, q.isEmpty(), a)) {
return;
}
for (;;) {
boolean d = done;
T v;
try {
v = q.poll(); //队列中取出参数
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.dispose();
q.clear();
a.onError(ex);
worker.dispose();
return;
}
boolean empty = v == null;
if (checkTerminated(d, empty, a)) {
return;
}
if (empty) {
break;
}
a.onNext(v); //真正onNext的代码
}
missed = addAndGet(-missed);
if (missed == 0) {
break;
}
}
}
...
@Override
public void run() { //为了线程调度
if (outputFused) {
drainFused();
} else {
drainNormal();
}
}
...
}
是通过队列传递参数,onNext等方法运行在worker线程调度中。这就是Subscriber 的线程控制的原理。
map起到类型转换的作用
public final Observable map(Function super T, ? extends R> mapper) {
ObjectHelper.requireNonNull(mapper, "mapper is null");
return RxJavaPlugins.onAssembly(new ObservableMap(this, mapper));
}
返回ObservalbeMap
public final class ObservableMap extends AbstractObservableWithUpstream {
final Function super T, ? extends U> function;
public ObservableMap(ObservableSource source, Function super T, ? extends U> function) {
super(source);
this.function = function;
}
@Override
public void subscribeActual(Observer super U> t) {
source.subscribe(new MapObserver(t, function));
}
static final class MapObserver extends BasicFuseableObserver {
final Function super T, ? extends U> mapper;
MapObserver(Observer super U> actual, Function super T, ? extends U> mapper) {
super(actual);
this.mapper = mapper;
}
@Override
public void onNext(T t) {
...
U v;
try {
v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
} catch (Throwable ex) {
fail(ex);
return;
}
actual.onNext(v);
}
...
}
}
通过内部类MapObserver完成对应工作,拿onNext举例,通过转换函数对参数进行转换再传递到上层的Oberver。
也是转换参数,不过转换成了Observable对象
public final Observable flatMap(Function super T, ? extends ObservableSource extends R>> mapper,
boolean delayErrors, int maxConcurrency, int bufferSize) {
...
return RxJavaPlugins.onAssembly(new ObservableFlatMap(this, mapper, delayErrors, maxConcurrency, bufferSize));
}
实现功能的是ObservalbeFlatMap
public final class ObservableFlatMap extends AbstractObservableWithUpstream {
...
@Override
public void subscribeActual(Observer super U> t) {
...
source.subscribe(new MergeObserver(t, mapper, delayErrors, maxConcurrency, bufferSize));
}
static final class MergeObserver extends AtomicInteger implements Disposable, Observer {
final Observer super U> actual;
final Function super T, ? extends ObservableSource extends U>> mapper;
...
final AtomicReference[]> observers;
static final InnerObserver, ?>[] EMPTY = new InnerObserver, ?>[0];
static final InnerObserver, ?>[] CANCELLED = new InnerObserver, ?>[0];
...
}
static final class InnerObserver extends AtomicReference
implements Observer
...
}
实现功能的有MergeObserver和InnerObserver两个内部类。MergeObserver会转换参数为InnerObserver,并持有对应的InnerObserver列表。例如同时下载五张图片,每张图片在不同的线程下载,可以将每一张图片转换成一个Observable,这样后续就可以方便的进行线程控制。
just可以合并同类的多个对象
public static Observable just(T item1, T item2) {
ObjectHelper.requireNonNull(item1, "The first item is null");
ObjectHelper.requireNonNull(item2, "The second item is null");
return fromArray(item1, item2);
}
just有多个版本,参数的个数不一样,但是最终都会调用到fromArray
public static Observable fromArray(T... items) {
ObjectHelper.requireNonNull(items, "items is null");
if (items.length == 0) {
return empty();
} else
if (items.length == 1) {
return just(items[0]);
}
return RxJavaPlugins.onAssembly(new ObservableFromArray(items));
}
实现具体功能的是ObservableFromArray
public final class ObservableFromArray extends Observable {
final T[] array;
public ObservableFromArray(T[] array) {
this.array = array;
}
@Override
public void subscribeActual(Observer super T> s) {
FromArrayDisposable d = new FromArrayDisposable(s, array);
s.onSubscribe(d);
...
d.run();
}
static final class FromArrayDisposable extends BasicQueueDisposable {
final Observer super T> actual;
final T[] array;
...
void run() {
T[] a = array;
int n = a.length;
for (int i = 0; i < n && !isDisposed(); i++) {
T value = a[i];
if (value == null) {
actual.onError(new NullPointerException("The " + i + "th element is null"));
return;
}
actual.onNext(value);
}
if (!isDisposed()) {
actual.onComplete();
}
}
}
}
通过FromArrayDisposable中的for循环实现对同一类Observable的控制。
connatMap和flatMap的不同之处在于可以保证代码执行的顺序性
public final Observable concatMap(Function super T, ? extends ObservableSource extends R>> mapper, int prefetch) {
...
return RxJavaPlugins.onAssembly(new ObservableConcatMap(this, mapper, prefetch, ErrorMode.IMMEDIATE));
}
public final class ObservableConcatMap extends AbstractObservableWithUpstream
@Override
public void onNext(T t) {
if (done) {
return;
}
if (fusionMode == QueueDisposable.NONE) {
queue.offer(t);
}
drain();
}
...
}
雷同,只拿内部类中的onNext举例,看出是使用队列保存参数然后再处理,保证了代码执行的顺序性
zip可以等待多个Observable全部完成
public static Observable zip(
ObservableSource extends T1> source1, ObservableSource extends T2> source2, ObservableSource extends T3> source3,
ObservableSource extends T4> source4,
Function4 super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) {
...
return zipArray(Functions.toFunction(zipper), false, bufferSize(), source1, source2, source3, source4);
}
public static Observable zipArray(Function super Object[], ? extends R> zipper,
boolean delayError, int bufferSize, ObservableSource extends T>... sources) {
...
return RxJavaPlugins.onAssembly(new ObservableZip(sources, null, zipper, bufferSize, delayError));
}
实现功能的是ObservableZip
public final class ObservableZip extends Observable
public void subscribeActual(Observer super R> s) {
...
ZipCoordinator zc = new ZipCoordinator(s, zipper, count, delayError);
zc.subscribe(sources, bufferSize);
}
static final class ZipCoordinator extends AtomicInteger implements Disposable {
...
public void subscribe(ObservableSource extends T>[] sources, int bufferSize) {
ZipObserver[] s = observers;
int len = s.length;
for (int i = 0; i < len; i++) {
s[i] = new ZipObserver(this, bufferSize);
}
// this makes sure the contents of the observers array is visible
this.lazySet(0);
actual.onSubscribe(this);
for (int i = 0; i < len; i++) {
if (cancelled) {
return;
}
sources[i].subscribe(s[i]);
}
}
...
public void drain() {
if (getAndIncrement() != 0) {
return;
}
int missing = 1;
final ZipObserver[] zs = observers;
final Observer super R> a = actual;
final T[] os = row;
final boolean delayError = this.delayError;
for (;;) {
for (;;) {
...
R v;
try {
v = ObjectHelper.requireNonNull(zipper.apply(os.clone()), "The zipper returned a null value");
//依据所有数据做转换
} catch (Throwable ex) {
...
}
a.onNext(v);
Arrays.fill(os, null);
}
missing = addAndGet(-missing);
if (missing == 0) {
return;
}
}
}
...
}
static final class ZipObserver implements Observer {
final ZipCoordinator parent;
final SpscLinkedArrayQueue queue;
...
ZipObserver(ZipCoordinator parent, int bufferSize) {
this.parent = parent;
this.queue = new SpscLinkedArrayQueue(bufferSize);
}
@Override
public void onSubscribe(Disposable s) {
DisposableHelper.setOnce(this.s, s);
}
@Override
public void onNext(T t) {
queue.offer(t);
parent.drain();
}
...
}
...
}
内部类有ZipCoordinator和ZipObserver,ZipCoordinator创建了多个ZipObserver,在ZipObserver的onNext中向队列传递了参数,然后后续交给ZipCoordinator。如果所有工作都完成,则zip完成使命推出无限循环。
链式调用,Android中最常见的可能就是AlertDialog
new AlertDialog.Builder(self)
.setTitle("确认")
.setMessage("确定吗?")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.show();
AlertDialog原理很简单,看下源码
public Builder setTitle(CharSequence title) {
P.mTitle = title;
return this;
}
就是每个方法都要return回自身对象,在AlertDialog.Builder来说就是AlertDialog.Builder
当然rxjava的架构复杂的多:
1.对Rxjava中的Observable operator来说就是return Observable的子类,上面所分析的所有Observable operator都是返回Observable对象的,具体来说是Observable的某个子类。
2.每个Observable都会实现
protected void subscribeActual(Observer super T> observer)
每个subscribeActual都会生成一个新的内部类对象observer作为subscribe方法的参数,例如ObservableObserveOn是ObserveOnObserver,ObservableSubscribeOn是SubscribeOnObserver。然后直接或者间接调用源Observable的subscribe方法,拿map operator举例:
public void subscribeActual(Observer super U> t) {
source.subscribe(new MapObserver(t, function));
}
这个是最清晰的subscribeActual架构,其中MapObserver是ObservableMap的内部类,source是ObservableMap构造方法中传入的源Observable。链式调用的代码会一直向下依次调用各层Observable的subscribeActual方法,最终到最底层,在本文demo中就是最开始创建的ObservableCreate。注意最核心的是把每层的内部类Observer都包装一下然后作为参数向下传递。到最底层实际上就开始调用用户实现的代码了,用户的代码中一般是对onNext等的调用。
subscribeActual调用顺序是从上到下,但是对用户来说代码的执行流程应该是从下往上呀,接下来就展现rxjava最巧妙的地方。
3.各层Observable内部类对象Observer必然有个内部成员(后续的Observer指的是内部类对象,不是指用户实现的Observer)
final Observer super T> actual;
它指向了上一层传入的Observer参数,最经典的Observer实现,例如
@Override
public void onNext(T t) {
...
actual.onNext(v);
}
每一层Observable在做完本层的工作后,又会调用上一层的Observer直到最顶层,这个和subscribeActual的调用顺序正好相反的。所以Observable的每层工作基本都是在内部类Observer中实现,这样才能实现rxjava的链式代码的执行顺序。
实质上就是通过参数传递的方式,依次入栈,然后在出栈,逆转了代码执行的顺序。注意subscribe()实际上还是逆序的,但是观察者回调的顺序是和代码书写顺序一致的。例如ObservableMap等的功能都实现在onNext中,而不是subscribeActual中,这样顺序才会对。由于subscribe()实际上还是逆序的,所以书写代码还是要注意点的,例如之前提及的线程控制方法:
.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
observeOn只对书写在下面的代码的OnNext等回调有效;
subscribeOn比较复杂,对书写在上面的代码的subscribe方法(从代码看subscribeOn方法附近的的onNext等回调也会跑在和subscribe同样的线程,这个和subscribeOn的本意还是稍微有区别的)有效。
针对subscribeOn举个例子,当然只是为了说明代码线程控制,日常不大可能两次改变subscribe的线程。
1.create
...
...
...
2.subscribeOn(thread 1)
...
...
...
...
3.subscribeOn(thread 2)
subscribe方法,从3->2的线程是thread 2,2->1的线程是thread 1;
理解了subscribeActual(从上到下)和内部类Observer(从下到上)的调用流程后理解上面例子应该是很容易的。
理解了Rxjava的原理后,看出架构太nb了。计算机的一切问题可以加个中间层来解决,在Rxjava的世界中变成变换个Observable就能解决。Observable类中已经有n多的operator了,日常开发是足够了,但是还是不满足怎么办,Rxjava还是有终级的解决方案的,就是lift:
public final Observable lift(ObservableOperator extends R, ? super T> lifter) {
ObjectHelper.requireNonNull(lifter, "onLift is null");
return RxJavaPlugins.onAssembly(new ObservableLift(this, lifter));
}
public final class ObservableLift extends AbstractObservableWithUpstream {
/** The actual operator. */
final ObservableOperator extends R, ? super T> operator;
public ObservableLift(ObservableSource source, ObservableOperator extends R, ? super T> operator) {
super(source);
this.operator = operator;
}
@Override
public void subscribeActual(Observer super R> s) {
Observer super T> observer;
try {
observer = ObjectHelper.requireNonNull(operator.apply(s), "Operator " + operator + " returned a null Observer");
} catch (NullPointerException e) { // NOPMD
...
}
source.subscribe(observer);
}
}
public interface ObservableOperator {
/**
* Applies a function to the child Observer and returns a new parent Observer.
* @param observer the child Observer instance
* @return the parent Observer instance
* @throws Exception on failure
*/
@NonNull
Observer super Upstream> apply(@NonNull Observer super Downstream> observer) throws Exception;
}
实现ObservableOperator,一切都可解决。不过对于大多数芸芸众生来说,理解透彻现有的operator也不是容易的事,光Observable就有13932行代码。