Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ServiceExecutor terminating condition

I'm new to java executor stuff.

I'm using Java's ExecutorService to launch several threads to process data.

Executor executor = Executors.newFixedThreadPool(poolSize);

for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i));

once the threads don't find data, they gracefully terminate.

My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully?

in case executor thread still runs, how can I let it terminate once all its child threads are done (poolSize number of threads).

like image 582
Java Spring Coder Avatar asked Nov 30 '25 16:11

Java Spring Coder


2 Answers

Executor will keep running with the underlying thread pool. You can still execute or submit a new task. It is recommended to shutdown Executor service call ExecutorService.html#shutdown() which attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

You can also use ExecutorService.html#shutdownNow() which stops all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. It is useful when you need immediate shutdown.

like image 91
Subhrajyoti Majumder Avatar answered Dec 02 '25 05:12

Subhrajyoti Majumder


1) threads in a fixed thread pool executor never terminate once started

2) there is no master thread in thread pool executor

like image 31
Evgeniy Dorofeev Avatar answered Dec 02 '25 06:12

Evgeniy Dorofeev