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

Chinmay Venkata Sabbam
art of coding
Published in
4 min readNov 24, 2019

Why Thread Pool is Required ?

In Java, we are implementing the threads to run tasks Asynchronously in Java.

Invoking thread in java
Thread invoking structure for the above code snippet

if you consider the above code snippet we can clearly understand that we need to invoke the threads, to run the tasks Asynchronously in Java. if we need to run the 100 tasks asynchronously in java, we need to invoke the 100 threads……

Running 100 tasks asynchronously
Threads invoking structure for the above code snippet

Is it right way to Implement in Real Time ?????

No, it is not the right way to implement like this, to invoke 100 threads to run 100 tasks asynchronously. Because these threads are operating system (OS) threads. If you invoke more OS threads simultaneously, most of threads are blocked because they are always depends up on the number of cores in CPU. if most number of threads are blocked. then blocking factor is increases. if blocking factor increases , it may leads to dead lock condition.

Then How we can achieve this use case to run 100 tasks asynchronously in Java ???. to solve this problem Java introduces the Executor Thread pool in Java 1.5

How the Executor thread pool in java Handles ……

Executor thread pool will create the n number of threads, based on the Executor Type we are using .

if we submit the m number of tasks, all the these m number of tasks will stored in the queue.

Initially n number of threads fetches n number of tasks from queue and executes in parallel. As soon as task completed by the thread, it immediately picks the anthor task from the queue, until the tasks completed in the queue.

Note : Type of the queue is changed based on the type of the Executor thread pool we are using .

if we ask Executor thread pool to create 10 threads and submitted 100 tasks to the Executor thread pool. these 100 tasks will stored in queue. Initially these 10 threads will execute 10 tasks in parallel. as soon as the task completed by the thread, it picks the anthor task stored in queue. In this manner all the 100 tasks will executed by the Executor thread pool asynchronously.

code snippet of the Executor Thread pool
Executor Thread Pool structure in Java

All the threads what we are creating is not an OS Threads but How many threads we need to create with the help of Executor Thread pool to execute all the tasks. How was the number of threads decided ?

How many Threads we need to create with the help of Executor Thread Pool ???

The number of threads we need to create was based on the type of the tasks we are submitting to the Executor Thread pool.

Generally tasks is of Two types

  1. CPU intensive based Operations
  2. IO intensive based Operations
Number of Threads <= (Number of cores in CPU)/(1- Blocking factor)
0<= Blocking factor< 1

if blocking factor = 0 then

Number of Threads <= Number of cores in CPU

if your task is an CPU Intensive operations like hashing and cryptographic applications, then blocking factor is must and should be 0 i.e Number of threads does not exceed the Number of cores in CPU.

If you give a 1000 threads to 10,000 to CPU intensive based operation tasks and if you have a 4 core CPU. Even if you give 1000 threads, it will only take 4 . it used time slicing between the threads.If thread-1 is executing and after some time it bump it off to the anthor Thread.So having too many threads is not a good idea at that time. If the operation is the CPU based intensive operation.

for CPU based intensive operations the number of threads = number of cores in CPU

if blocking factor between 0 and 1 (i.e is 0.9)

Number of Threads = 2(Number of Cores In CPU)

if your task is an IO intensive based Operation then we can choose the number of threads based on the IO based operations we are performing , we need to analyze the capacity of Database.Even it is greater than the number of cores, it does not cause any problems.

if blocking factor =1 then your code will face DEADLOCK CONDITION

Types of an Executor Thread pool

There are four types of Executor Thread pool

  • Fixed Thread-pool
  • Cached Thread-pool
  • Scheduled Thread-pool
  • Single-threaded Executor

we can learn these types of Executor Thread pools in detail in part-2 , stay tuned …..

--

--