我试图了解rxjava合并的工作原理。所以这里是简单的代码,它应该合并来自2个observable的结果并发送给订阅者
Observable.merge(getObservable(), getTimedObservable())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override public void call(final String s) {
Log.i("test", s);
}
});
private Observable<String> getTimedObservable() {
return Observable.interval(150, TimeUnit.MILLISECONDS)
.map(new Func1<Long, String>() {
@Override public String call(final Long aLong) {
Log.i("test", "tick thread: " + Thread.currentThread().getId());
return String.valueOf(aLong);
}
});
}
public Observable<String> getObservable() {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override public void call(final Subscriber<? super String> subscriber) {
try {
Log.i("test", "simple observable thread: " + Thread.currentThread().getId());
for (int i = 1; i <= 10; i++) {
subscriber.onNext(String.valueOf(i * 100));
Thread.sleep(300);
}
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
});
}
我预计订阅者的合并结果会像
100 0 1 200 2 300 4 5 400
或类似的东西,但实际结果是:
test: simple observable thread: 257
test: 100
test: 200
test: 300
test: 400
test: 500
test: 600
test: 700
test: 800
test: 900
test: 1000
test: tick thread: 254
test: 0
test: tick thread: 254
test: 1
test: tick thread: 254
test: 2
test: tick thread: 254
test: 3
test: tick thread: 254
test: 4
test: tick thread: 254
test: 5
test: tick thread: 254
test: 6
test: tick thread: 254
test: 7
test: tick thread: 254
test: 8
test: tick thread: 254
test: 9
test: tick thread: 254
test: 10
test: tick thread: 254
test: 11
test: tick thread: 254
test: 12
test: tick thread: 254
test: 13
看起来Thread.Sleep在第一个Observable块中以第二个可观察的方式发出,但我不明白如何。有人可以解释一下吗?
答案 0 :(得分:3)
merge将同时订阅两个observable。将首先订阅的observable将在调用线程上生成值。因为调用线程被observable1阻塞,所以observable2不能生成值。 SubscribeOn只会说出订阅的位置。让我们说可观察开始在main-1上产生价值。下游的每个值都将位于同一个线程上。没有并发发生。
如果您想要实现并发性,则必须为每个可观察的订阅者说明订阅必须发生的位置。所以我们假设Observables.merge有两个可观察对象。 Observable1和Observable2订阅了一些Threadpool。每个observable都将在subscribeOn的给定线程上生成值。你实现了并发。
Plase查看编辑后的输出:
@Test
public void name() throws Exception {
Subscription subscribe = Observable.merge(getObservable(), getTimedObservable())
//.observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> {
System.out.println("subscription " + s);
//Log.i("test", s);
});
Thread.sleep(5_000);
}
private Observable<String> getTimedObservable() {
return Observable.interval(150, TimeUnit.MILLISECONDS)
.map(aLong -> {
System.out.println("getTimedObservable: " + Thread.currentThread().getId());
//Log.i("test", "tick thread: " + Thread.currentThread().getId());
return String.valueOf(aLong);
}).subscribeOn(Schedulers.io());
}
private Observable<String> getObservable() {
return Observable.<String>create(subscriber -> {
try {
for (int i = 1; i <= 10; i++) {
System.out.println("getObservable: " + Thread.currentThread().getId());
subscriber.onNext(String.valueOf(i * 100));
Thread.sleep(300);
}
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}).subscribeOn(Schedulers.io());
}
答案 1 :(得分:0)
默认情况下,RxJava不是多线程的,并且所有内容都在同一个线程上运行。如果需要多线程,则需要使用调度程序。
在.subscribe(Subscribers.io())
的末尾添加getObservable()
。