RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

onBackpressureBlock() blocks forever when subscribe and observe on newThread

Open FinalVersion opened this issue 11 years ago • 2 comments

        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?

FinalVersion avatar Feb 13 '15 03:02 FinalVersion

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.

akarnokd avatar Feb 13 '15 07:02 akarnokd

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!

FinalVersion avatar Feb 13 '15 08:02 FinalVersion