3

Here is my code snippet.

ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);
while(conditionTrue)
{
ClassImplementingRunnable c = new ClassImplementingRunnable();
executor.submit(c);
}

Now after this is do

executor.shutdown();

What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor.

But i guess this is not what is happening here. The main thread seems to be executing shutdown and it just shuts down everything.

Before when my threadpool size was 2, i did the following and it seemed to work.

ClassImplementingRunnable c1 = new ClassImplementingRunnable();
executor.submit(c1);
ClassImplementingRunnable c2 = new ClassImplementingRunnable();
executor.submit(c2);
Future f1 = executor.submit(c1);
Future f2 = executor.submit(c2);
while(!f1.done || !f2.done)
{}
executor.submit();

How do i do this for a greater number of threads in the threadpool? Thanks.

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 1
    This won't work if you have two threads as both tasks could be performed by the same thread while the other is still busy. – Peter Lawrey Aug 30 '12 at 09:13

3 Answers3

14

You generally use the following idiom:

executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
  • shutdown just says that the executor won't accept new jobs.
  • awaitTermination waits until all the tasks that have already been submitted finish what they are doing (or until the timeout is reached - which won't happen with Integer.MAX_VALUE - you might want to use a lower value).
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
assylias
  • 321,522
  • 82
  • 660
  • 783
  • what is the MAX_VALUE and SECONDS? – Kraken Aug 30 '12 at 09:13
  • @Kraken It is the timeout - so it you use `executor.awaitTermination(1, TimeUnit.SECONDS);` it will wait at most 1 second for example. – assylias Aug 30 '12 at 09:14
  • i am not sure how long will it take? So i just put in a large number randomly? – Kraken Aug 30 '12 at 09:14
  • @Kraken If you want to make sure you wait until everything is done, yes. I have added links to the javadoc. Alternatively, you can use Marko's solution if you don't want to wait more than a certain amount of time. – assylias Aug 30 '12 at 09:16
  • but it still wont be as efficient as waiting till all the processes have finished, and ending immediately after that.? – Kraken Aug 30 '12 at 09:18
  • 1
    @Kraken awaitTermination will return as soon as all the threads have finished - it is equivalent to your code with `while(!f1.done || !f2.done) {}`. – assylias Aug 30 '12 at 09:18
  • so if i say MAX_VALUE=1000 and the threads finish execution after 10 seconds it waits for only 10 seconds? Thanks – Kraken Aug 30 '12 at 09:20
8

shutdown is a "soft" order and won't abruptly shut down the executor. It will do just what you want: refuse any new tasks, wait for all submitted tasks to complete, and then shut down.

Add awaitTermination to your code in order to block until the executor service is shut down.

In the documentation there's also this code that covers all the angles:

void shutdownAndAwaitTermination(ExecutorService pool) {
   pool.shutdown(); // Disable new tasks from being submitted
   try {
     // Wait a while for existing tasks to terminate
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
       pool.shutdownNow(); // Cancel currently executing tasks
       // Wait a while for tasks to respond to being cancelled
       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
           System.err.println("Pool did not terminate");
     }
   } catch (InterruptedException ie) {
     // (Re-)Cancel if current thread also interrupted
     pool.shutdownNow();
     // Preserve interrupt status
     Thread.currentThread().interrupt();
   }
 }
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor.

That is what executor.shutdown(); does. If you want to shutdown immediately you need to use shutdownNow()

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130