Java ExecutorService - Part 2 - Type of Pools
Learn how to parallelize your tasks and operations easily in Java without cooking up your own Threads.
Part 1: Introduction - https://www.youtube.com/watch?v=6Oo-9Can3H8
Part 2: Type of Pools - https://www.youtube.com/watch?v=sIkG0X4fqs4
Part 3: Constructor / LifeCycle - https://www.youtube.com/watch?v=Dma_NmOrp1c
Part 4: Callable/Future - https://www.youtube.com/watch?v=NEZ2ASoP_nY
Channel
----------------------------------
Complex concepts explained in short & simple manner. Topics include Java Concurrency, Spring Boot, Microservices, Distributed Systems etc. Feel free to ask any doubts in the comments. Also happy to take requests for new videos.
Subscribe or explore the channel - https://youtube.com/defogtech
New video added every weekend.
Popular Videos
----------------------------------
What is an API Gateway - https://youtu.be/vHQqQBYJtLI
Executor Service - https://youtu.be/6Oo-9Can3H8
Introduction to CompletableFuture - https://youtu.be/ImtZgX1nmr8
Java Memory Model in 10 minutes - https://youtu.be/Z4hMFBvCDV4
Volatile vs Atomic - https://youtu.be/WH5UvQJizH0
What is Spring Webflux - https://youtu.be/M3jNn3HMeWg
Java Concurrency Interview question - https://youtu.be/_RSAS-gIjGo
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
So Java provides four kinds of thread pools.
First is a fixed thread pool, which we saw earlier.
Second is a cached thread pool.
Third is a scheduled thread pool.
And fourth is single threaded executor.
So first one, the fixed thread pool,
is exactly the one that we saw earlier.
Wherein, it has fixed number of threads,
which in this case is T0 to T9, so 10 threads.
And you keep submitting the tasks to it.
All the tasks that you submit,
are stored in a particular queue,
That queue has to be thread safe. So typically it's a blocking queue and all these threads will fetch
tasks from the queue and execute it one after the other.
and the code for it is also what we saw earlier
you do execute.newFixedThreadPool
you give it the size of the pool and you submit the tasks
using the for loop or otherwise
The next type of thread pool is slightly different
Here, you do not have a fixed number of threads
and you also do not have a queue
which will hold the number of tasks that you submit
Instead, the queue is replaced by something called synchronous queue which has only space
for a single item.
So every time you submit a particular task, the pool will hold that task in this location
and it will search for any of the threads which are already created and which are free
to operate or execute that task.
If no such thread is available, then it will create a new thread
It will add it to the pool and it will ask that thread to execute the task
So now you can imagine so instead of
10 we have 100 tasks instead of 100 if we have 1000 tasks then theoretically all
10 threads will be busy when the 11th task is submitted
So that's when it will create T10, which is the 11th thread and so on and so forth until T999
So theoretically it can create
1000 threads to execute all the tasks. So since that's too many threads, cache thread pool also has the ability
To kill those threads once they have been idle for more than 60 seconds
So if a thread is no longer required if there are no other tasks that are coming in
then this thread pool will kill those threads. So that's when your thread pool size will slowly start shrinking and
The code for that is very similar to fixed size thread pool
But instead you do not pass in an argument for the threads
So the method call is executor.newCacheThreadPool() and it doesn't take any arguments
Because you are not in control of number of threads that it creates and the task submission remains the same
The third kind of thread pool is called scheduled thread pool
Here it is specifically for the kind of tasks that you want to schedule after a certain delay
Okay, so let's say you want to
Perform some kind of checks security checks logging checks some kind of checks after every day
10 seconds. You can use these calls of this service dot schedule and you can
give it a delay of say 10 seconds or if you want to perform some checks every 10
seconds you can use some method schedule at a fixed rate of 10 seconds. What it
does is it will store all the tasks that you submit in the queue but that queue is