简体   繁体   English

RxJava2可观察到的背压

[英]RxJava2 Observable backpressure

recently I realized that I don't understand how RxJava2 backpressure works. 最近我意识到我不明白RxJava2背压是如何工作的。

I made small test and I expect that it should fail with MissingBackpressureException exception: 我做了一个小测试,我希望它会因MissingBackpressureException异常而失败:

@Test
public void testBackpressureWillFail() {
    Observable.<Integer>create(e -> {
        for (int i = 0; i < 10000; i++) {
            System.out.println("Emit: " + i);
            e.onNext(i);
        }
        e.onComplete();
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(Schedulers.computation())
    .doOnNext(i -> {
        Thread.sleep(100);
        System.out.println("Processed:" + i);
    })
    .blockingSubscribe();
}

System out shows next: 系统输出显示下一个:

Emit: 0
Emit: 1
Emit: 2
...
Emit: 10000

Processed:0
Processed:1
Processed:2
...
Processed:10000

Why it doesn't produce MissingBackpressureException . 为什么它不会产生MissingBackpressureException

I expect that e.onNext(i); 我希望e.onNext(i); will put item into buffer of ObservableObserveOn and after it's size is greater than static final int BUFFER_SIZE = Math.max(16,Integer.getInteger("rx2.buffer-size",128).intValue()); 将item放入ObservableObserveOn缓冲区中,并且它的大小大于static final int BUFFER_SIZE = Math.max(16,Integer.getInteger("rx2.buffer-size",128).intValue());

It should throw MissingBackpressureException which doesn't happen. 它应该抛出不会发生的MissingBackpressureException Does the buffer automatically grow? 缓冲区会自动增长吗? If not where are items stored? 如果不存储物品的位置?

That's because backpressure moved out to Flowable only with RxJava2, see here . 这是因为背压搬到了Flowable只有RxJava2,看这里
If you will switch to Flowable with BackpressureStrategy.MISSING you will get the exception. 如果您将使用BackpressureStrategy.MISSING切换到Flowable ,您将获得异常。
That also means that in your case you indeed have buffer that automatically grows, from observerOn docs: 这也意味着在你的情况下你确实有自动增长的缓冲区,来自observerOn docs:

Modifies an ObservableSource to perform its emissions and notifications on a specified Scheduler, asynchronously with an unbounded buffer ... 修改ObservableSource以在指定的Scheduler上执行其发射和通知,与无界缓冲区异步...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM