0

オブジェクトのコレクションがあります。このオブジェクトのコレクションに対して、Future を返すメソッドを呼び出す必要があります。現在get()、操作を同期させるために、Future を使用しています。どうすれば非同期に変換できますか?

for (Summary summary : summaries) {
    acmResponseFuture(summary.getClassification()));
    String classification = summary.getClassification();
    // this is a call which return Future and which is a sync call now
    AcmResponse acmResponse = acmResponseFuture(classification).get();
    if (acmResponse != null && acmResponse.getAcmInfo() != null) {
        summary.setAcm(mapper.readValue(acmResponse.getAcmInfo().getAcm(), Object.class));

    }
    summary.setDataType(DATA_TYPE);
    summary.setApplication(NAME);
    summary.setId(summary.getEntityId());
    summary.setApiRef(federatorConfig.getqApiRefUrl() + summary.getEntityId());
}
4

1 に答える 1

0

Future同期呼び出しを待機する前に、すべてのインスタンスを収集するのはどうですか?

    Collection<Future<AcmResponse>> futures = new ArrayList<>();
    for (Summary summary : summaries) {
        acmResponseFuture(summary.getClassification()));
        String classification = summary.getClassification();
        // this is a call which return Future...
        futures.add(acmResponseFuture(classification));
    }

    for (Future<AcmResponse> future : futures) {
        // ...and which is a sync call now
        AcmResponse acmResponse = future.get();
        if (acmResponse != null && acmResponse.getAcmInfo() != null) {
            summary.setAcm(mapper.readValue(acmResponse.getAcmInfo().getAcm(), Object.class));

        }
        summary.setDataType(DATA_TYPE);
        summary.setApplication(NAME);
        summary.setId(summary.getEntityId());
        summary.setApiRef(federatorConfig.getqApiRefUrl() + summary.getEntityId());
    }

明らかに、要約の更新を整理する必要があります。しかし、アイデアは、呼び出しを行う前に、すべての先物を一度に取得したいということです。先物と要約を地図に入れ...

于 2016-03-03T20:31:13.397 に答える