Mycode는 다음과 같습니다. 두 개의 try ... catch 블록이 너무 추합니다.지정된 시간 내에 다중 선물을 처리하는 우아한 방법이 있습니까?
두 요청이 있습니다. 하나의 응답이 시간 초과 기간 내에 목록에 추가되면, 목록에 null을 추가합니다.
private List<String> getResult(Future future1, Future future2, long timeout) {
String resp1= null;
String resp2= null;
Stopwatch stopwatch = Stopwatch.createStarted();
try {
resp1= future1.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.error("req1 error", e);
}
// calculate the time remaining
long leftTime = timeout - stopwatch.elapsed(TimeUnit.MILLISECONDS);
if (leftTime > 0) {
try {
resp2 = future2.get(leftTime, TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.error("req2 error", e);
}
}
ArrayList<String> results = Lists.newArrayListWithCapacity(2);
results.add(resp1);
results.add(resp2);
return results;
}