Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
How to stop a Callable thread process after a exception in one thread?

I'm using Callable and ExecutorService to use multi-threading in my application.

In case if any Exceptions are thrown from one thread need to stop all threads even though it's work completed and need to throw that exception to the calling class method. For this, I used a shutDownNow() method.

Is this the right way? or any other effective ways there?

        
ExecutorService exSvc = Executors.newFixedThreadPool(5);
exSvc.setKeepAliveTime(60, TimeUnit.SECONDS);

List<Future<Integer>> futureList = new LinkedList();
for(int i=0; i<50;i++){
    futureList.add(exSvc.submit(
        new Callable<Integer>() {
            public Integer call()  throws Exception{
                int num =  new Random().nextInt(1000);
                if(num==500){
                    throw new Exception("Error");
                }
            return num;
            }
        }));
}

for(int i=0; i<50; i++){
 
   try {
    int value =  futureList.get(i).get();
   } catch (Exception e) {
        exSvc.shutdownNow();
        throw new Exception("Error");
   } 
}


over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This should work in your example. However, be sure to properly handle thread interruptions in your callables.

Note that the other tasks will be cancelled on a best effort basis and only after you have noticed that one of the tasks have thrown an exception. E.g., if task no. 5 have thrown an exception, you have to wait for the completion of tasks 0-4, and only then you notice that task 5 has failed.

Alternatively, you could subclass the ThreadPoolExecutor like here, to detect the exception as early as possible.

over 4 years ago · Santiago Trujillo Relatório

0

As ciamei said, if you want to detect the exception as early as possible, maybe you can use a CountDownLatch:

        CountDownLatch cdl = new CountDownLatch(1);
        AtomicInteger successCount = new AtomicInteger();
        AtomicBoolean fail = new AtomicBoolean();
        int taskSize = 50;
        for(int i=0; i<taskSize;i++){
            futureList.add(exSvc.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int num =  new Random().nextInt(1000);
                    if(num == 500){
                        fail.set(true);
                        cdl.countDown();
                        throw new Exception("Error");
                    }
                    if (successCount.incrementAndGet() == taskSize) {
                        cdl.countDown();
                    }
                    return num;
                }
            }));
        }
        try {
            cdl.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        if (fail.get()) {
            exSvc.shutdownNow();
            throw new Exception("Error");
        }
        //...
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda