0

我有一个对象集合。我必须在这个返回 Future 的对象集合上调用一个方法。现在我使用get()on 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 回答