Deeper insights of Thread-pool & Need of Asynchronous programming in Java ….(part-2)

Chinmay Venkata Sabbam
art of coding
Published in
5 min readNov 26, 2019

Before reading this blog for the better understanding please go through

Types of Executor Thread Pools

1. Fixed Thread-pool

Fixed Thread pool
Thread structure for the fixed thread pool

Working :

  1. As per the above code snippet we are running 100 tasks asynchronously . using 5 threads
  2. Fixed thread pool creates 5 threads and it uses Blocking Unbounded queue which stores 100 tasks.
  3. Initially 5 threads picked 5 tasks from blocking queue and execute them parallel. As soon as the task completed by one thread, it pick’s up the anthor task from the blocking queue. In this way 5 threads executes 100 tasks Asynchronously.

2.Cached Thread-pool

Cached Thread Pool
Thread Structure for the Cached Thread Pool

Working :

  1. In Cached pool there is no fixed number of threads and It consists of synchronous queue which hold 1 task

2. if there are 100 tasks, if 10 threads are busy when 11 th tasks is submitted 11 th thread is created i.e means t1 to t100 threads are created .

if Thread is idle for 60 seconds then thread pool can kill the tasks i.e means slowly thread pool starts shrinking

3.Synchronized Thread Pool

synchronized thread pool
Thread Structure for the Scheduled Thread Pool

Working:

  1. It is the thread pool which is used to schedule the tasks at the Time delay

Eg : security checks or login checks

2. Tasks are placed in the Delay Queue.It does not mean that tasks are running in the sequential order, it executes in parallel

3. If two tasks t1 and t2 and you will schedule the t1 task after 20 minutes and t2 task after 5 minutes then t2 task is executes first and then t1 executes

4. (1) schedule () →Task after a certain delay

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);  scheduledExecutorService.schedule(()->  System.out.println(Thread.currentThread().getName()+"Task to Run After 10 Seconds Delay"),10,TimeUnit.SECONDS);

In the above code, Task will run after 10 seconds . this task will execute only one time

(2) scheduleAtFixedRate() →tasks which are run at every specific interval of time

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
scheduledExecutorService.scheduleAtFixedRate(()-> System.out.println(Thread.currentThread().getName()+"Task to run repeatedly after every 10 seconds after 70 seconds delay"), 70, 10, TimeUnit.SECONDS);

In the above code, Task will execute repeatedly after every 10 seconds after 75 seconds delay

(3) scheduleAtFixedDelay() →task which run after the certain interval based on the previous task completes

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
scheduledExecutorService.scheduleWithFixedDelay(()-> System.out.println(Thread.currentThread().getName()+"Task to run repeatedly after every 10 seconds after previous tasks completes after 100 seconds delay"), 80, 10, TimeUnit.SECONDS);

In the above code, Task will execute after every 10 seconds when the previous tasks completes,after 80 seconds delay

5. Scheduled Thread Pool can create any number of Threads, if it is required

4. Single Threaded Executor

Single Thread pool executor
Thread structure for Single Threaded Executor

Working:

  1. Single Threaded Executor consists of only one thread.

2. If thread is killed due to the Exception, The pool creates the anthor thread and tasks are executed which are present in the blocking queue . these threads executes sequentially.

Internal Implementation of the Executor Thread Pool

Internal Implementation of the Executor Thread Pool

Parameters Description :

constructor parameters
Thread pool sizes

Core pool size = initial size

Current pool size = threads added/deleted based on pool type and keep a live time

sizes of the thread pools

Note: core pool threads are never killed unless allowCoreThreadTimeOut(boolean value) is set to be true

Types of Queues used in Different Thread Pools

Different Queues used by the different thread pools

Rejection Handler

if you design a custom Thread Pool. There is a chances that tasks are rejected by the Thread Pool, because it is implemented by using the Array Blocking Queue.

Tasks rejects by the thread pool
Output of the above program

In the above program , size of the Array Blocking queue is 300 and Number of tasks submitting to the thread Pool is 500. initial size is 10 and maximum pool size is 100.

when threads are busy in running the first 100 tasks , remaining 300 tasks are present in queue. when we are submitting the 401 th task to the queue. there is no place to store the tasks. so it throws an Rejection Handler Exception.

Handling Rejection Handler Exceptions

Handling Custom Rejection Handlers
Output of the above program

we can handle the RejectedExecutionExceptions by creating the Custom Rejection Handler as shown as above code

Custom Rejection Handler with lambda Expressions

(runnable,executor) -> System.out.println("I am Handling CustomRejection Handler")

Custom Rejection Handler with Boiler plate code


private static class CustomRejection Handler implements RejectedExecutionHandler {

@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){
// perform any operations
}

Life Cycle of the Executor Thread Pool

  1. executor.shutdown() :

It initiates the shutdown process , but it does not accept other tasks after calling this , but it can complete all the tasks which are present in blocking queue

If we submit the task after calling this , we can face a Rejection Execution Exception

2. executor.isshutdown() :

to check shutdown() is initiated or not

3. executor.isTerminated():

it will return true if all tasks are completed including queued ones

4. executor.awaitTermination(10,TimeUnit.Seconds)

block until all tasks are completed or until the timeout occurs

5. List<Runnable> runnables = executor.shutdownNow()

it will stop all the tasks abruptly . but it returns all the tasks which are queued in blocking queue

Life cycle of a thread Pool Executor

In the Next Part we will discuss about the Future and what use-case Future could not solve and improvements of the Java 8 in Asynchronous Programming and how we can implement the Custom Blocking pool without facing memory issues etc… stay tuned …..

--

--