Saturday 8 March 2014

Executor Interface

Executor is in java.util.concurrent package.

Executor interface has one method execute(), all the class that implements this interface provides the implementation for this method.

void execute(Runnable command)
Executes the given command at some time in the future. Depends on the Executor implementation, this method can execute in a pooled thread, new thread or calling thread.

ThreadPoolExecutor class implements Executor interface, will see the use of execute() by below example.

WorkerThread.java
class WorkerThread implements Runnable{
 String taskName;

 WorkerThread(String taskName){
  this.taskName = 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);
  }
 }
}

SimpleThreadPool.java
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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

  WorkerThread tasks[] = new WorkerThread[10];
  for(int i=0; i<10; i++){
   tasks[i] = new WorkerThread("task" + i);
   exec.execute(tasks[i]);
  }
 }
}

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
pool-1-thread-4 executing the task task3
pool-1-thread-5 executing the task task4
pool-1-thread-2 completed the task task1
pool-1-thread-3 completed the task task2
pool-1-thread-3 executing the task task6
pool-1-thread-4 completed the task task3
pool-1-thread-4 executing the task task7
pool-1-thread-5 completed the task task4
pool-1-thread-5 executing the task task8
pool-1-thread-1 completed the task task0
pool-1-thread-1 executing the task task9
pool-1-thread-2 executing the task task5
pool-1-thread-4 completed the task task7
pool-1-thread-3 completed the task task6
pool-1-thread-5 completed the task task8
pool-1-thread-2 completed the task task5
pool-1-thread-1 completed the task task9

Executor 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.

After executing the tasks also program waits for execution, since the threads are not shut downed. They are waiting in the thread pool for new tasks.   
   






Thread Pools                                                 Callable Interface                                                 Home

No comments:

Post a Comment