i'm trying to run a for loop on parrallel using 4 threads and I have to stop all other threads when it gets to an else, basically the code looks like this:
CompletableFuture<ResponseEntity<TramRealTimeResponse>> responseEntityCompletableFuture = null;
ExecutorService pool = Executors.newFixedThreadPool(4);
for (TramRulesDTO rule : tramRulesResponse.getPayload()) {
responseEntityCompletableFuture = CompletableFuture.supplyAsync(() ->
{
logger.info("thread " + Thread.currentThread().getName());
TramRule tramRule = TramRuleFactory.getTramRule(rule.getRuleId());
PartnerRuleDto partnerRuleDto = getPartnerRule(partnerRuleResponse.getPayload(), rule.getRuleId());
if (Objects.nonNull(tramRule)) {
try {
tramRule.doTramLogicAndReturnAction(rule, partnerRuleDto,
paymentDetails, intermediaryDetails, partnerRulesConditions, tramRulesRequest);
} catch (TramRealTimeException ex) {
TramRealTimeResponse response = exceptionHandler.handleTramRealTimeException(ex, partnerRuleDto);
if (response.getStatus().equals(200)) {
return null;
} else {
//Stop all threads
return ResponseEntity.ok(response);
}
}
} else {
return null;
}
return null;
}, pool);
}
ResponseEntity<TramRealTimeResponse> tramRealTimeResponseResponseEntity = responseEntityCompletableFuture.get();
Where is the //Stop all threads comment I need somehow to stop all other threads and retrieve the ResponseEntity object down after the for loop. I've tried with executor service shutDown() and shutDownAll() and it doesn't stop anything