[英]RxJava queue multi observables
我有很多Observable方法。 我希望在之前的完成后繼續運行。
public Observable<String> method1(){
// complex stuff (10 sec)
}
public Observable<String> method2(){
// another complex stuff (10 sec)
}
foo.method1().subscribe(...);
foo.method2().subscribe(...);
foo.method1().subscribe(...);
foo.method1().subscribe(...);
我想一個接一個地運行它...而那個最難的一個:我不能改變B類 。 我必須改變A級才能實現我的目標。 有任何想法嗎?
我可以使用flatMat(...)
或concatMap(...)
在B類中完成,但我無法更改B類
它看起來很糟糕,但應該做的工作。 Sempahore將確保只有一個線程正在處理數據。 請注意,此解決方案不保證操作順序。
public class A {
private final Semaphore semaphore = new Semaphore(1);
public Observable<String> method1() {
try {
semaphore.acquire();
// complex stuff (10 sec)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public Observable<String> method2() {
try {
semaphore.acquire();
// another complex stuff (10 sec)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.