依存関係を持つオブジェクトを作成しようとしています。ポイントは、ExecutorServiceを持つクラスとRunnableを生成するクラスが異なることです。ここでは、単純な抽象化です:ExecutorServiceに渡すことができ、実行可能ファイル間の依存関係を持つオブジェクト
public class Main {
private ExecutorService pool; // Initialized before executing main
public static void main(String[] args) {
List<Batch> batches = // fetching...
for(Batch batch : batches) {
Runnable r = batch.getRunnable();
pool.submit(r);
}
}
}
public class Batch {
public Runnable getRunnable() {
Runnable r1 = // creating...
Runnable r2 = // creating...
// FIXME: demand that r2 run after r1 finishes
return // something suitable. r1? r2? or new Runnable?
}
}
これらのクラスが1のとき、私はCompletableFutureを使用するために使用される:
CompletableFuture.runAsync(r1, pool)
.thenRunAsync(r2, pool)
.exceptionally(ex -> { // Do something });
しかし、今pool
は別のクラスに存在します。私はCompletableFutureクラスのドキュメントをもっと見ていますが、まだそれが助けになるかどうかはわかりません。
誰もがここで何か知識を持っていますか?
あなたの 'Batch'が' Runnable'のリストを返せば十分ではないでしょうか?そして、あなたが以前使ったように 'runAsync'と' thenRunAsync'を使うことができます。 – samjaf
このように実装することもできますが、上記が達成されれば、整然とした柔軟な実装になると思います。それは奇妙な要件ですか? – tsuda7