更新同一订阅中的 flatMap 并发限制

Updating flatMap concurrent limit in the same subscription

我有一个 Android 服务,它在 PublishSubject 通过 EventBus 接收下载事件时下载文件,我想根据设置限制并发下载的数量。

实例化服务时,它会创建 PublishSubject 和以下订阅:

PublishSubject<DownloadEvent> downloadsSubject = PublishSubject.create();

Subscription downloadSubscription = downloadsSubject
                .subscribeOn(Schedulers.io())
                .filter(event -> !isDownloaded(event))
                .flatMap(this::addDownloadToQueue)
                .flatMap(this::startDownload, preferences.getDownloadThreadsNumber())
                .onBackpressureBuffer()
                .subscribe();

但是设置是在订阅的时候才获取的,修改设置是没有效果的

有没有办法为下一个队列发射更新此值(或其他方法)而无需再次订阅?

Here is 具有自定义运算符的可运行 class,应该可以满足您的需求。

在这种情况下有多种竞态条件,我已尝试涵盖其中的大部分。操作员不协调背压,因此您可能需要 onBackpressureBuffer.