Ad

Wednesday 4 September 2013

Queuing strategies for ThreadPoolExecutor in Java

In ThreadPoolExecutor queues are used to hold the submitted task, if there are no ideal threads to
run/allocate the task.There are three strategies for the Queuing.
  • Direct Handoffs
  • Unbounded Queues
  • Bounded Queues
we will use the same program written  in the post ThreadPoolexecutor in Java and we will change only the method  createThreadPool() to implement one of the Queuing strategy.Depending on the Queue we use in the ThreadPoolexecutor,the behavior of the threadpool will vary as described in the below sections.

Direct Handoffs:
   In this queuing strategy the queue will not hold any submitted task   and it will always create the threads upto maxpoolsize when there are no ideal threads(CorePoolSize threads are busy) in the Threadpool. Once the maxpoolsize threads also becomes busy,submission of new task will be rejected and can be handled via Rejection Handler.SynchronousQueue can be used for the Direct Handoffs.when the maxpoolsize is defined as more and the time consuming task is arriving faster,there is a possibility more number of threads will be running in the pool.

private void createThreadPool() {                                                                                                            
int poolSize=2;
int maxPoolSize=5;
long aliveTive=1000;
SynchronousQueue<Runnable> queue= new SynchronousQueue<Runnable>();
threadPoolExecutor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
               TimeUnit.MILLISECONDS,queue,new JobRejectionHandler());
}

Output for Direct Handoffs:
JobId:Job6 Running through RejectionHandler,Since there is no ideal threads in ThreadPool
JobId:Job7 Running through RejectionHandler,Since there is no ideal threads in ThreadPool
JobId:Job8 Running through RejectionHandler,Since there is no ideal threads in ThreadPool
JobId:Job9 Running through RejectionHandler,Since there is no ideal threads in ThreadPool
JobId:Job10 Running through RejectionHandler,Since there is no ideal threads in ThreadPool
JobId:Job1 Running through Thread:pool-1-thread-1
JobId:Job2 Running through Thread:pool-1-thread-2
JobId:Job3 Running through Thread:pool-1-thread-3
JobId:Job4 Running through Thread:pool-1-thread-4
JobId:Job5 Running through Thread:pool-1-thread-5

As we can see in the above output.
  • Thread-1 and Thread-2 were allocated to run the Task Job1 and Job2.So the corepoolsize threads(i.e 2) are busy in running this task.
  • Once the Job3 is submitted,it is not queued as we used the Direct Handoff.It therefore constructs the thread-4.The same apply for Job4 and Job5 as well which were run by Thread-4 and Thread-5.
  • Since there were no ideal threads in ThreadPool(Since the maxpoolsize is 5 and all 5 threads were busy),submission of task Job6 to Job10 were rejected and were run by the Rejectionhandler. 

Unbounded Queues:
  In this queuing strategy the submitted task will wait in the queue if the corePoolsize theads are busy and the task will be allocated if any of the threads become idle.Thus ThreadPool will always have number of threads running  as mentioned in the corePoolSize.Thus the maxPoolSize will not have any effect in threadpool size. LinkedBlockingQueue without the capacity can be used for this queuing strategy.If the corePoolsize of the threadpool is less and there are more number of time consuming task were submitted,there is more possibility that the task has to wait in the queue for more time before it is run by any of the ideal thread.Also no task will be rejected in Threadpool until the threadpool was shutdown.

private void createThreadPool() {                                                                                                            
int poolSize=2;
int maxPoolSize=5;
long aliveTive=1000;
LinkedBlockingQueue<Runnable> queue= new LinkedBlockingQueue<Runnable>();
threadPoolExecutor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
               TimeUnit.MILLISECONDS,queue,new JobRejectionHandler());
}


Output for unbounded Queues:
JobId:Job1 Running through Thread:pool-1-thread-1
JobId:Job2 Running through Thread:pool-1-thread-2
JobId:Job3 Running through Thread:pool-1-thread-1
JobId:Job4 Running through Thread:pool-1-thread-2
JobId:Job5 Running through Thread:pool-1-thread-1
JobId:Job6 Running through Thread:pool-1-thread-2
JobId:Job7 Running through Thread:pool-1-thread-1
JobId:Job8 Running through Thread:pool-1-thread-2
JobId:Job9 Running through Thread:pool-1-thread-1
JobId:Job10 Running through Thread:pool-1-thread-2

As we can see in the above output,
  • Thread-1  and Thread-2 were allocated to run the Task Job1 and Job2. So the corepoolsize threads(i.e 2) are busy in running this task.
  • The forthcoming submitted task job3,Job4 etc will wait in the queue.It will be run by either Thread 1 or Thread 2 when they become idle.
  • Thus only 2 threads will run in the threadpool and the maxPoolsize which was defined as 5 will not have any effect to the threadPool size.  

Bounded Queues:
 We already used this queuing strategy in the example written in post ThreadPoolexecutor in Java .The submitted task will wait in the queue if there are no ideal corepoolsize threads.Once the queue is full,the threads will be created upto maxpoolsize for every submitted task.ArrayBlockingQueue can be used for this queuing strategy.

private void createThreadPool() {                                                                                                            
int poolSize=2;
int maxPoolSize=5;
  int queueSize=3;
long aliveTive=1000;
ArrayBlockingQueue<Runnable> queue= new ArrayBlockingQueue<Runnable>(queueSize);
threadPoolExecutor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
               TimeUnit.MILLISECONDS,queue,new JobRejectionHandler());
}

Output for unbounded Queues:
JobId:Job1 Running through Thread:pool-1-thread-1
JobId:Job2 Running through Thread:pool-1-thread-2
JobId:Job9 Running through RejectionHandler,Since there are no ideal threads in ThreadPool
JobId:Job10 Running through RejectionHandler,Since there are no ideal threads in ThreadPool
JobId:Job6 Running through Thread:pool-1-thread-3
JobId:Job7 Running through Thread:pool-1-thread-4
JobId:Job8 Running through Thread:pool-1-thread-5
JobId:Job3 Running through Thread:pool-1-thread-1
JobId:Job4 Running through Thread:pool-1-thread-2
JobId:Job5 Running through Thread:pool-1-thread-3

Related Topics:
ThreadPoolexecutor in Java
Rejected Handler Policies for ThreadPoolExecutor

1 comment: