我正在使用RxJava 2和改造库,并希望执行700多个查询,其中每个查询都是一个改进的API调用。
如何使用zip操作符在RXJava 2中实现此目的?
答案 0 :(得分:0)
700或1000万没关系,你通常有一个8核的CPU,所以只有8个并行处理它将被执行。
您可以将flatMap与MaxConcurrent一起使用
/**
* FlatMap operator allow you to specify the max number of concurrent operation that this operator can process.
* This is another way to make back pressure if you know your system cannot process more than specific number
* of elements in the pipeline.
*/
@Test
public void asyncFlatMapWithMaxConcurrent() {
Observable.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))//Your number of request
.flatMap(value -> Observable.just(value)
.map(number -> {
System.out.println("Make your request here");
return number;
}).subscribeOn(Schedulers.newThread())
, 8)//Max concurrent operations
.subscribe();
new TestSubscriber()
.awaitTerminalEvent(15, TimeUnit.SECONDS);
}
您可以在此处查看有关RxJava的更多示例https://github.com/politrons/reactive