1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Science & Technology
  6. >
  7. Java ExecutorService - Part 2 - Type of Pools

Java ExecutorService - Part 2 - Type of Pools

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: Defog Tech
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
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:00
So Java provides four kinds of thread pools.
0:03
First is a fixed thread pool, which we saw earlier.
0:06
Second is a cached thread pool.
0:08
Third is a scheduled thread pool.
0:10
And fourth is single threaded executor.
0:12
So first one, the fixed thread pool,
0:15
is exactly the one that we saw earlier.
0:17
Wherein, it has fixed number of threads,
0:20
which in this case is T0 to T9, so 10 threads.
0:23
And you keep submitting the tasks to it.
0:26
All the tasks that you submit,
0:28
are stored in a particular queue,
0:29
That queue has to be thread safe. So typically it's a blocking queue and all these threads will fetch
0:35
tasks from the queue and execute it one after the other.
0:40
and the code for it is also what we saw earlier
0:43
you do execute.newFixedThreadPool
0:46
you give it the size of the pool and you submit the tasks
0:49
using the for loop or otherwise
0:52
The next type of thread pool is slightly different
0:57
Here, you do not have a fixed number of threads
1:01
and you also do not have a queue
1:04
which will hold the number of tasks that you submit
1:06
Instead, the queue is replaced by something called synchronous queue which has only space
1:14
for a single item.
1:16
So every time you submit a particular task, the pool will hold that task in this location
1:26
and it will search for any of the threads which are already created and which are free
1:32
to operate or execute that task.
1:34
If no such thread is available, then it will create a new thread
1:39
It will add it to the pool and it will ask that thread to execute the task
1:45
So now you can imagine so instead of
1:48
10 we have 100 tasks instead of 100 if we have 1000 tasks then theoretically all
1:56
10 threads will be busy when the 11th task is submitted
2:00
So that's when it will create T10, which is the 11th thread and so on and so forth until T999
2:08
So theoretically it can create
2:10
1000 threads to execute all the tasks. So since that's too many threads, cache thread pool also has the ability
2:18
To kill those threads once they have been idle for more than 60 seconds
2:23
So if a thread is no longer required if there are no other tasks that are coming in
2:28
then this thread pool will kill those threads. So that's when your thread pool size will slowly start shrinking and
2:37
The code for that is very similar to fixed size thread pool
2:41
But instead you do not pass in an argument for the threads
2:46
So the method call is executor.newCacheThreadPool() and it doesn't take any arguments
2:52
Because you are not in control of number of threads that it creates and the task submission remains the same
3:00
The third kind of thread pool is called scheduled thread pool
3:06
Here it is specifically for the kind of tasks that you want to schedule after a certain delay
3:13
Okay, so let's say you want to
3:15
Perform some kind of checks security checks logging checks some kind of checks after every day
3:21
10 seconds. You can use these calls of this service dot schedule and you can
3:27
give it a delay of say 10 seconds or if you want to perform some checks every 10
3:33
seconds you can use some method schedule at a fixed rate of 10 seconds. What it
3:40
does is it will store all the tasks that you submit in the queue but that queue is
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome