Sunday, 9 March 2014

awaitTermination

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. "timeout" represents the maximum time to wait and "unit" represens the time unit of the timeout argument.
  
class WorkerThread implements Runnable{
 String taskName;

 WorkerThread(String taskName){
  this.taskName = taskName;
 }

 public String toString(){
  return taskName;
 }

 public void run(){
  try{
   Thread t1 = Thread.currentThread();
   System.out.println(t1.getName() + " executing the task " + taskName);
   t1.sleep(1000);
   System.out.println(t1.getName() + " completed the task " + taskName);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.*;
   
class SimpleThreadPool{
 public static void main(String args[])throws Exception{
  ExecutorService exec = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

  WorkerThread tasks[] = new WorkerThread[10];
  for(int i=0; i<5; i++){
   tasks[i] = new WorkerThread("task" + i);
   exec.execute(tasks[i]);
  }
  exec.shutdown();
  System.out.println("Execution Completed");
 }
}

Sample Output
pool-1-thread-2 executing the task task1
pool-1-thread-1 executing the task task0
pool-1-thread-3 executing the task task2
Execution Completed
pool-1-thread-1 completed the task task0
pool-1-thread-1 executing the task task3
pool-1-thread-2 completed the task task1
pool-1-thread-2 executing the task task4
pool-1-thread-3 completed the task task2
pool-1-thread-2 completed the task task4
pool-1-thread-1 completed the task task3
   
ExecutorService exec = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
Above statement create a thread pool of size 5. So it can run 5 tasks at a time, Since 5 threads are reside in the pool.

exec.submit(tasks[i])
Submit the tasks to the thread pool. When the task is submitted, ThreadPool assigns a thread to execute the run method of the Runnable Object.

As you observe the output, "Execution Completed" statement executed immediately after calling the shutdown, I.e, shutdown won't block the execution of current thread, until all the tasks finish. To block the thread, until all the tasks finishes, use "awaitTermination".

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.*;

class SimpleThreadPool{
 public static void main(String args[])throws Exception{
  ExecutorService exec = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

  WorkerThread tasks[] = new WorkerThread[10];
  for(int i=0; i<5; i++){
   tasks[i] = new WorkerThread("task" + i);
   exec.execute(tasks[i]);
  }
  exec.shutdown();
  exec.awaitTermination(10, TimeUnit.SECONDS);
  System.out.println("Execution Completed");
 }
}
  
Sample Output
pool-1-thread-1 executing the task task0
pool-1-thread-3 executing the task task2
pool-1-thread-2 executing the task task1
pool-1-thread-1 completed the task task0
pool-1-thread-2 completed the task task1
pool-1-thread-2 executing the task task3
pool-1-thread-3 completed the task task2
pool-1-thread-3 executing the task task4
pool-1-thread-2 completed the task task3
pool-1-thread-3 completed the task task4
Execution Completed
  



Check whether executor shutdown or not                                                 Submit Callable task                                                 Home

No comments:

Post a Comment