Sandip
Sandip

Reputation: 347

What happens to remaining thread of invokeAny Executor Service

InWhen invokeAny successfully returns, what happens to remaining threads? Does it get killed automatically? If not how can I make sure that thread is stopped and return back to threadpool

ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.invokeAny(callables);

Upvotes: 0

Views: 265

Answers (2)

the Hutt
the Hutt

Reputation: 18398

Just elaborating more on the topic.

What happens to remaining threads

If the treads are executing methods which throw InterruptedException then they receive the exception. Otherwise they get their interrupted flag set to true.

Does it get killed automaticlly?

Not really.
- If they are running in infinite loop then you need to make sure you do not swallow InterruptedException and exit the thread in the catch block.
- If you are not expecting the exception then you need to keep checking flag using Thread.interrupted() or Thread.currentThread().isInterrupted() and exit when it's true.
- If you are not running infinite loop then the threads will complete their tasks and stop. But their results will not be considered.

In following code both task, and task2 keep running even the service is stopped and main method exits:


public class Test {
    public static void main(String[] args) throws Exception {
        Callable<String> task1 = () -> {
            for (;;) {
                try {
                    Thread.sleep(9000);
                    System.out.println(Thread.currentThread().getName()+
                            " is still running..");
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() 
                            + " has swallowed the exception.");
                    //it is a good practice to break the loop here or return
                }
            }
        };
        Callable<String> task2 = () -> {
            for(;;) {
                if(Thread.interrupted()) { 
                    //it is a good practice to break the loop here or return
                    System.out.println(Thread.currentThread().getName()+
                            " is interrupted but it is still running..");
                }
            }
        };
        List<Callable<String>> tasks = List.of(task1, task2, () -> "small task done!");
        ExecutorService service = Executors.newFixedThreadPool(4);
        String result = service.invokeAny(tasks);
        System.out.println(result);
        
        service.shutdownNow();
        System.out.println("main thread done");
    }

}

Output:

small task done!
pool-1-thread-2 is interrupted but it is still running..

pool-1-thread-1 has swallowed the exception.

pool-1-thread-1 has swallowed the exception.
main thread done
pool-1-thread-1 is still running..
pool-1-thread-1 is still running..

Upvotes: 1

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

Upon calling the method invokeAny they are all cancelled/stop when the remaining threads are not yet completed.

Here is the documentation of it:

   Upon normal or exceptional return, tasks that have not completed are cancelled. 

Upvotes: 0

Related Questions