我有一个代码,我需要并行调用不同的服务,所以我使用了可完成的未来来并行调用它,但是如果 5 个线程中有 1 个线程抛出异常
如果其中一个线程出现异常,如何检查该线程是否分配回公共池
如何将该特定线程分配回池中,以便它可以被重用并且该线程不会被阻塞
public void populateBinCenterDetails(List<CardsManageDto> cardsDtoList) {. //main function
List<String> binRequestList = getBinListForBinCenterQuery(cardsDtoList);
List<PG2BinInfo> binInfos = new ArrayList<>();
int binCenterTimeout = Integer.parseInt(CardServicePropertiesUtil.getInstance().getProperty(
CommonConstant.BIN_CENTER_TIMEOUT));
if (CollectionUtils.isEmpty(binRequestList)) {
return;
}
try{
List<CompletableFuture<List<PG2BinInfo>>> futureResultList = Lists.newArrayList();
for (List<String> binRequests : Lists.partition(binRequestList, BIN_CENTER_BATCH_QUERY_SIZE)) {
futureResultList.add(getCompetableFutureResult(binRequests));
}
CompletableFuture[] futureResultArray = futureResultList.toArray(new CompletableFuture[futureResultList.size()]);
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(futureResultArray);
CompletableFuture<List<List<PG2BinInfo>>> finalResults = combinedFuture
.thenApply(voidd ->
futureResultList.stream()
.map(future -> future.join())
.collect(Collectors.toList()));
List<List<PG2BinInfo>> listBinlist = finalResults.get(binCenterTimeout, TimeUnit.MILLISECONDS);
for (List<PG2BinInfo> temp : listBinlist) {
binInfos.addAll(temp);
}
}
catch (CompletionException | ExecutionException | InterruptedException | TimeoutException e) {
throw new CardServiceException(CommonResultCode.SYSTEM_ERROR, "Call failed to new Bin center ");
}
Map<String, PG2BinInfo> binInfoMapping = new HashMap<>();
binInfos.forEach(bin -> binInfoMapping.put(bin.getBin(), bin));
cardsDtoList.forEach(card ->
fillBinInfo(binInfoMapping.get(card.getTokenBin()), card));
}
}
//here i am calling the external service parallely and collecting the result in future and returning to main function
public CompletableFuture<List<PG2BinInfo>> getCompetableFutureResult(List<String> binRequests) {
CompletableFuture<List<PG2BinInfo>> future1=CompletableFuture.supplyAsync(() -> externalQueryHelper.fetchBinCenterInfoList(binRequests));
return future1;
}
我有一个代码,我需要并行调用不同的服务,所以我使用了可完成的未来来并行调用它,但是如果 5 个线程中有 1 个线程抛出异常,我如何检查该线程是否被分配回公共池,如果其中一个线程出现异常 有没有什么方法可以检查可完成的未来,或者它会被分配回池而不会中断,有或没有例外
请提出方法或方法