RxJavaで2つのステップを連続して実行したい。私は別のAPIのバリエーションをしようとしているとRxJavaが並列に私の2つのステップを実行している、私が望む動作ではありませんれているRxJava 2入門:タスクを順次実行します。 `と` `や` `延期?
step 1: start
step 1: finish
step 2: start
step 2: finish
:
step 1: start
step 2: start
step 2: finish
step 1: finish
Iは、ステップ1は、ステップの前のような2開始を終了したいです
以下のコードサンプルでは、私はandThen
とdefer
の両方を試して、私はパラレル実行を取得します。どのように私はこれを修正することができますので、1つのステップは、他の正常終了後に実行されますか?
メソッド名andThen
は、順次シリアル実行を意味します。メソッドdefer
は、私が望むシリアルタスクの実行に期待するメソッドシグネチャである別のCompletableを生成する関数を取ります。どちらも、私が望む結果を私に与えてはいけません。
Observable/Flowableに変換する必要がありますか?または、Completableで2つのステップをチェーンすることはできますか?
public class RxStep1Then2 {
public static Completable simulateCompletable(ScheduledExecutorService es, String msg, int msDelay) {
System.out.println(String.format("%s: start", msg));
ScheduledFuture<?> future = es.schedule(() -> {
System.out.println(String.format("%s: finish", msg));
}, msDelay, TimeUnit.MILLISECONDS);
return Completable.fromFuture(future);
}
public static void rxMain(ScheduledExecutorService es) {
// Completable c = simulateCompletable(es, "step 1", 1000)
// .andThen(simulateCompletable(es, "step 2", 500));
Completable c = simulateCompletable(es, "step 1", 1000)
.defer(() -> simulateCompletable(es, "step 2", 500));
c.blockingAwait();
System.out.println("blockingAwait done");
}
public static void main(String[] args) throws Exception {
ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
System.out.println("Started ExecutorService.");
rxMain(es);
es.shutdown();
es.awaitTermination(5, TimeUnit.MINUTES);
System.out.println("Shutdown ExecutorService. Done.");
}
}
私は、 'simulateCompletable'がタスクを開始することを知っています。私は、APIがラムダを取った 'andThen'の何らかの変形を提供し、最初のタスクが完了するまで2番目のタスクが開始されないことを期待していました。私はそのようなAPIを見つけることができません。また、私はこれに遅れを加えたくありません。 – clay
'.andThen(Completable.defer(() - > simulateCompletable()))')を使用します。 – akarnokd
'andThen(Completable.defer(() - > ...)'それを修正しました!ありがとう! – clay