1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Science & Technology
  6. >
  7. Java ExecutorService - Part 3 - Constructor & LifeCycle methods

Java ExecutorService - Part 3 - Constructor & LifeCycle methods

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 ---------------------------------- 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
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:01
Now let's look at the slightly advanced topics for the
0:05
executor service. So in the previous videos we looked at how to create a new
0:10
fixed thread pool and the other types of pool and the method we called for that
0:15
was very straightforward. So what we use is a static method called
0:19
executors.newFixedThreadPool or executors.newCachedThreadPool and
0:25
that creates the thread pool for us. Internally if you look at the source
0:29
code, all it does is it just calls a constructor for the thread pool executor.
0:35
This constructor of course accepts this list of parameters.
0:40
For every different type of thread pool, the actual arguments which are passed are slightly different.
0:46
Let's look at the list of these parameters.
0:50
The first and the second is the core pool size, max pool size, and related to that is the keep-alive timeout.
0:57
We'll look at how it affects the core and max pool size in a bit.
1:02
The next bit is about the work queue.
1:05
This is the blocking queue in which all the tasks are stored.
1:09
Then there is a thread factory which is the factory used to create new threads in case
1:14
the pool needs new threads.
1:16
And the last but not the least is a rejected execution handler.
1:20
This is the callback or an handler method which is used to handle the rejections when
1:26
you submit the tasks to the queue and the thread pool is unable to accept those tasks.
1:33
So going back to the first two parameters which was a core pool size
1:37
and the max pool size. The core pool size is the initial size or the base size for
1:44
a thread pool and based on the type of pool if the thread pool needs to add more
1:49
threads it will add so and increase the value of the pool size. In that case the
1:56
current pool size will be slightly different from the core pool size. The
2:00
max pool size determines how the upper threshold, the max pool size determines
2:05
the upper threshold for pool size to be. And the keep alive time is the time for
2:12
which if a thread remains idle and if there are no other tasks then the thread
2:18
pool can kill the threads. So in that case instead of expanding the thread
2:23
pool can contract or it can shrink. So number of threads can slowly be deleted
2:28
and removed from the thread pool. So in that case the pool size will slowly
2:33
shrink down to core pool size and it will never go below the core pool size.
2:39
Right, so let's look at what the values are for four types of thread pool which
2:44
we saw in the earlier videos. So for the fixed thread pool the core pool size and
2:49
the max pool size are the same which is the constructor argument
2:52
passed and the keep-alive time is 0. 0 is slightly misleading, 0 is supposed to
2:58
be read as not applicable. Okay, so the keep-alive time for the fixed thread pool
3:02
is not applicable because there is no killing of threads or adding of new
3:06
threads apart from maintaining the core pool size. The next bit is the cached thread
3:13
pool. So initially it starts with 0 threads and it can go on to the maximum
3:19
number of threads allowable. So since the thread count is always an integer, so the
3:25
maximum number of threads you can have is integer.max value. And of course you do not
3:30
want to keep growing your pool size because all the threads will consume a lot of memory,
3:35
you will have a keeper lifetime. So the keeper lifetime in this case is 60 seconds. So if
3:40
a thread is idle for 60 seconds and there is no task assigned to it, then the thread
3:45
pool will kill that particular thread. In scheduled thread pool we have the base
3:51
core pool size as whatever is passed in the constructor argument and the max
3:55
pool size is the maximum of the integer value and here similarly we have a keep
4:02
a lifetime of 60 seconds. The last one is a single threaded executor which as the
4:09
name suggests will have a size of only one so that is why the core pool size
4:13
and the max pool size, both of them are 1.
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome