同期メソッド呼び出しからCompletableFutureを作成するために1ライナーが存在するかどうかを知りたい。いいえ、なぜですか?同期メソッド呼び出しからCompletableFutureを作成する
ロングバージョン:
final CompletableFuture<ReturnType> future = new CompletableFuture<>();
final String parameters = "hello";
ReturnType result;
try {
result = syncMethodCall(parameters);
} catch (Exception e) {
future.completeExceptionally(e);
}
future.complete(result);
return future;
ショート目的のバージョン(または種類):
final String parameters = "hello";
return CompletableFuture.superMethod(() -> {syncMethodCall(parameters)});
'CompletableFuture.completedFuture(syncMethodCall(parameters))' –
@SotiriosDelimanol'SyncMethodCall'が例外をスローすると、この例外はCompletableFutureに格納されません。 –
あなたの長いバージョンは問題ありません。それを短いバージョンに変換するには、Callableというタイプのパラメータを持つ独自の "superMethod"を定義するだけです。 –