- Tubelator AI
- >
- Videos
- >
- Science & Technology
- >
- Java ExecutorService - Part 3 - Constructor & LifeCycle methods
Java ExecutorService - Part 3 - Constructor & LifeCycle methods
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
----------------------------------
Master difficult programming concepts in few minutes. I try to explain difficult concepts like Java concurrency in simple to understand manner using animations and small code snippets. Explore videos on topics like Spring Boot, Cloud Foundry, Java 8 (with more coming soon). I am happy to clarify your doubts. Ask me anything in the comments. I am also happy to take requests for new videos.
New video added every Sunday.
Subscribe or explore the channel - http://bit.ly/defog_tech
Playlists
----------------------------------
Java Executor Service - http://bit.ly/exec_srvc
Java Concurrency - http://bit.ly/java_crncy
Spring Boot 2.0 - http://bit.ly/spr_boot2
Java 8 - http://bit.ly/java_8-11
Intellij IDEA Shortcuts - http://bit.ly/i_idea
Popular Videos
----------------------------------
Executor Service - https://youtu.be/6Oo-9Can3H8
Introduction to CompletableFuture - https://youtu.be/ImtZgX1nmr8
Understand how ForkJoinPool works - https://youtu.be/5wgZYyvIVJk
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
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
Now let's look at the slightly advanced topics for the
executor service. So in the previous videos we looked at how to create a new
fixed thread pool and the other types of pool and the method we called for that
was very straightforward. So what we use is a static method called
executors.newFixedThreadPool or executors.newCachedThreadPool and
that creates the thread pool for us. Internally if you look at the source
code, all it does is it just calls a constructor for the thread pool executor.
This constructor of course accepts this list of parameters.
For every different type of thread pool, the actual arguments which are passed are slightly different.
Let's look at the list of these parameters.
The first and the second is the core pool size, max pool size, and related to that is the keep-alive timeout.
We'll look at how it affects the core and max pool size in a bit.
The next bit is about the work queue.
This is the blocking queue in which all the tasks are stored.
Then there is a thread factory which is the factory used to create new threads in case
the pool needs new threads.
And the last but not the least is a rejected execution handler.
This is the callback or an handler method which is used to handle the rejections when
you submit the tasks to the queue and the thread pool is unable to accept those tasks.
So going back to the first two parameters which was a core pool size
and the max pool size. The core pool size is the initial size or the base size for
a thread pool and based on the type of pool if the thread pool needs to add more
threads it will add so and increase the value of the pool size. In that case the
current pool size will be slightly different from the core pool size. The
max pool size determines how the upper threshold, the max pool size determines
the upper threshold for pool size to be. And the keep alive time is the time for
which if a thread remains idle and if there are no other tasks then the thread
pool can kill the threads. So in that case instead of expanding the thread
pool can contract or it can shrink. So number of threads can slowly be deleted
and removed from the thread pool. So in that case the pool size will slowly
shrink down to core pool size and it will never go below the core pool size.
Right, so let's look at what the values are for four types of thread pool which
we saw in the earlier videos. So for the fixed thread pool the core pool size and
the max pool size are the same which is the constructor argument
passed and the keep-alive time is 0. 0 is slightly misleading, 0 is supposed to
be read as not applicable. Okay, so the keep-alive time for the fixed thread pool
is not applicable because there is no killing of threads or adding of new
threads apart from maintaining the core pool size. The next bit is the cached thread
pool. So initially it starts with 0 threads and it can go on to the maximum
number of threads allowable. So since the thread count is always an integer, so the
maximum number of threads you can have is integer.max value. And of course you do not
want to keep growing your pool size because all the threads will consume a lot of memory,
you will have a keeper lifetime. So the keeper lifetime in this case is 60 seconds. So if
a thread is idle for 60 seconds and there is no task assigned to it, then the thread
pool will kill that particular thread. In scheduled thread pool we have the base
core pool size as whatever is passed in the constructor argument and the max
pool size is the maximum of the integer value and here similarly we have a keep
a lifetime of 60 seconds. The last one is a single threaded executor which as the
name suggests will have a size of only one so that is why the core pool size
and the max pool size, both of them are 1.