RxJava
RxJava copied to clipboard
onBackpressureBlock() blocks forever when subscribe and observe on newThread
Observable<Integer> source = Observable.range(0,1000)
.onBackpressureBlock()
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.newThread());
source.subscribe(new Action1<Integer>() {
@Override
public void call(Integer i) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
The output gives only 0 to 127, where I expected it to be 0 to 999. This behavior exists in both 1.0.6 and master. Is this a bug or I used it incorrectly?
Hi. What's happening is that subscribeOn routes all backpressure requests to the same thread doing the range emission which blocks. You need to move the subscribeOn before the onBackpressureBlock.
Wow, I didn't notice such subtle difference. Though I haven't quite understood what really happens under the hood, it works anyway. Thank you!