How to exit ExecutorService after all the threads have done their execution?

Viewed 4865

This is the code which is working but I am specifying the wait time explicitly. Is there any way to exit ExecutorService when all the threads have completed their execution.

    ExecutorService es = Executors.newCachedThreadPool();
    {
      for(final List<String> list:partitions){

       es.execute(new Runnable() { 
         public void run() {
      try{
            System.out.println(list);
            new CallAPI().make_call(list, access_token);    

        }catch(Exception e){
        System.out.println(e);
        }       
      }
     });
    Thread.sleep(5000);
   }
   boolean finshed = es.awaitTermination(15, TimeUnit.MINUTES);
   es.shutdown();

boolean finshed = es.awaitTermination(15, TimeUnit.MINUTES);==>I am giving here wait time but I don't want this because I don't know when the thread will finish execution

4 Answers
Related