0

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

Truica Sorin
  • 499
  • 2
  • 11
  • 24
  • your threads need to cooperate with an interrupt, for example if you call `shutDownNow()` this will set the interrupt flag - but your threads need to check for that. In general, `CompletableFuture` has a method `cancel` - but it does not interrupt either. – Eugene Aug 04 '21 at 14:44
  • so i can't do anything? – Truica Sorin Aug 04 '21 at 14:45
  • if you want to interrupt some task - then start with a proper tool, `CompletableFuture` is not such. – Eugene Aug 04 '21 at 14:46
  • what do you recommend? :D – Truica Sorin Aug 04 '21 at 14:46
  • I suggest you [start reading here](https://stackoverflow.com/questions/29013831/how-to-interrupt-underlying-execution-of-completablefuture). In general, once you started something and want to "kill it", from java- it's not going to be easy. But that link has a library that says it works in solving that problem. – Eugene Aug 04 '21 at 14:54
  • I'll look into it thanks! – Truica Sorin Aug 04 '21 at 15:18

0 Answers0