Sunday 9 March 2014

RejectedExecutionException

Exception throws when a task cannot be accepted for execution.

For Example, once a shutdown(), called, then no new tasks are accepted for execution. Trying to execute a task, once the shutdown() called on ExecutorService object, throws RejectedExecutionException

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);
  }
 }
}
   
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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

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

  exec.shutdown();
  exec.execute(tasks[0]);
 }
}
   
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
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task    WorkerThread@65cc892e rejected from java.util.concurrent.ThreadPoolExecutor@57425f38[Shutting down, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
at SimpleThreadPool.main(SimpleThreadPool.java:29)
pool-1-thread-1 completed the task task0
pool-1-thread-3 completed the task task2
pool-1-thread-3 executing the task task3
pool-1-thread-1 executing the task task4
pool-1-thread-2 completed the task task1
pool-1-thread-3 completed the task task3
pool-1-thread-1 completed the task task4
   

Executor exec = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
Above statement create a thread pool of size 3. So it can run 3 tasks at a time, Since 3 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 in the above program
exec.shutdown();
exec.execute(tasks[0]);

eexecute method is called after calling the shutdown(), so RejectedExecutionException thrown.





Shutdown the thread pool                                                 Immediate shutdown of ThreadPool                                                 Home

No comments:

Post a Comment