Introduction: - The video discusses the different types of thread pools provided by Java's `ExecutorService`. Thread Pool Types: 1. Fixed Thread Pool: - Has a fixed number of threads. - All submitted tasks are stored in a blocking queue. - Threads fetch and execute tasks sequentially. - Ideal for CPU-intensive tasks. 2. Cached Thread Pool: - Does not have a fixed number of threads. - Uses a synchronous queue to hold tasks. - Creates threads as needed and can kill idle threads. - Suitable for IO-intensive tasks. 3. Scheduled Thread Pool: - Used for scheduling tasks to run after a certain delay or at fixed intervals. - Tasks are stored in a delay queue. - Provides methods for scheduling tasks once, at fixed rates, or with fixed delays. 4. Single Threaded Executor: - Similar to fixed thread pool but with only one thread. - Ensures tasks are executed sequentially. - Useful for tasks that require strict sequential execution. Summary: - Java's `ExecutorService` provides various types of thread pools for managing tasks efficiently. - The choice of thread pool depends on the nature of tasks, such as CPU-intensive or IO-intensive, and the scheduling requirements. - Understanding the characteristics of each thread pool helps in selecting the appropriate one for different scenarios.
the videos are amazing, the most interesting aspect is you connect cpu cores caches and the threads, hardware to software, great sir, please continue this you are really a guru thanks👍
Explanation was very good. I have one opinion, I might be wrong here but I don't think SingleThreadedExecutor executes task in sequential manner. It might pick up the tasks in order but it doesn't mean that it will pick it up after completing the first one. Because, in case first thread is doing some IO operation, the CPU and the thread will start processing the next task.
You are right. If one of the I/O task is waiting then single thread can continue to execute other tasks while waiting for the I/O task to complete. FIFO ordering is not guaranteed unless all tasks are CPU based.
Excellent explanation Deepak - request you to continue creating more and more videos covering all the major aspects like Collections - DS etc, Great work keep it up :) And thanks a ton !!
Great explanation.. simple and easy to grab and a quick bite to get a very holistic view about the executors framework. I have watched this and some other videos of this series and are really good. Just to add another difference b/w a "fixed-size thread pool" and a "single-threaded thread pool" is that in case of a "fixed-size thread pool", you can still update/re-configure the pool size set in the constructor later on using the setMaximumPoolSize() method of the ThreadPoolExecutor class.
I think one thing need to clarify is required between scheduleAtFixedRate and scheduleWithFixedDelay 1) The fixedRate run periodically, even though the last same execution is not finished, the pool will arrange another thread to proceed, so in this case there are two threads execute the same operation. 2) The fixedDelay will wait the last same operation to finish and then delay say 10 seconds to restart the same operation.
Done thanks Types of thread pools 1. Fixed thread pool 2. Cached thread pool - dynamically creates and terminates threads based on the number of tasks being submitted and wether there are idle threads (frequency of tasks being submitted) 3. Scheduled thread pool - uses a delay queue to keep the tasks, the tasks are not kept sequentially based on when they are submitted, but instead based on when they need to run ScheduledExecutorService .schedule(...) for running one after delay .scheduleAtFixedRate(...) running every x seconds .scheduleWithFixedDelay(...) schedule and wait x seconds after task completion 4. Single threaded executor - to ensure tasks are run in order
Thank you for tutorial it's very helpful to understand in detail Please explain about Threadpoolexeuter constructor Min Max Blookingqueue Exception Up to my knowledge Thanks in advance Expecting soon
Hi For a single threaded executor,do we have another advantage than actually running sequential task on a different core ,since such scenario can be achieved sequentially on the main thread as well I have understood it to be helping in a non blocking sequential execution of ordered tasks only
Hi Sir.. For newCachedThreadPool you have told theoretically it will create number of thread max upto number of task. But I have check it will exceeds.
Thank you very much for this clear explanation. I would like a little more detail on "scheduleAtFixedRate" method to clarify that if the run time of the task exceeds the rate period, is that an error or does the task simply start immediately again (I assume the latter).
FixedRate scheduler doesn't check finish times of the job, it will keep triggering new ones. The fixedDelay one will wait Post a job's completion before triggering next one
@@simonbaker9909 Depends on the threadpool size. If size is greater than 1, it will assign a new thread. So both tasks will overlap (will run in parallel)
Hello Sir, Can you please explain where we are using the blocking queue here for task submission? or is it internally from java that the submitted task will be stored in the blocking queue and will be executed by the scheduler one by one?
Thank you for detailed explanation. Although its 3 years old video, but if you get some time, kindly review: Is Delay Queue same as priority queue? Are there in retry options for Single Threaded Executor Service. Suppose the task 1 keeps on failing and we want to retry it just for n number of times and after that Task 2 should be tried.
Hi. Great explanation. Went directly into mind. One query. There should be some way to stop the given tasks like for ScheduledatFixedRate right. If i want to exexute a task with rate of 10 and it should end after 50 executions.
If you can't control the number of threads used in a cached thread pool how does it figure out the number of threads? Does it assign threads based on the CPU Cores?
How does in the scheduled pool, delay queue submit the task to the actual thread.(It search for free thread/ already every task is assigned to a thread/or something else)?
It's other way around. Threads will keep polling for tasks from delay queue like normal thread pool, but delay queue only shows tasks once their time has expired.
Clear and concise explanation!However,I have some doubts.What is the difference between a Singlethreaded pool and a Fixedpool with single thread? Also does SingleThreaded pool guarantee the execution of tasks in FIFO order?
Both Singlethreaded pool and a Fixedpool with single thread are the same. Internally SingleThreadPool used normal FixedSizedPool. Yes, thats correct, SingleThreadedPool will ensure FIFO order of execution.
@DefogTech Just a doubt!. Here in tha case of SingleThreadedExecutor, since only one Thread is there, no need of BlockingQueue right? normal Queue is sufficient.
Still need blocking queue, even though there is only 1 queue taking elements, there are 1 or more threads adding elements from outside to ExecutorService.. so it still requires thread safe queue
really like your videos , but I have one question , in case of Single Threaded Executor why thread pool is using blocked queue , if there is only one thread a normal queue will work the same way right ?
Generally we always use static factory methods of Executors class. There are some subtle differences between single-threaded and fixed-threadpool of size 1
@@DefogTech Ok, Now I see it. But in Your video there is no code snippet for static factory method for singleThreadedExecutor, only for fixed, cached and scheduled thread pools so I thought there is no such method.
Thank you for the clear explanation. I have a doubt - isn't the type 4 - "Single threaded executor" same as single threaded application. After all, everything will run in sequence..
yes, but you could have multiple pools in your application right? some can be multi-threaded some can be single-threaded. So at an application you still have many threads running concurrently.
Really thanks for putting up such high quality stuff. Coming to ScheduledThreadPool, is DelayQueue actually PriorityQueue - prioritizing the tasks based on execution time ??
I have a scenario where I need to convert around 50000 images to PNG format and then upload them to Box (using Java API's). Performing it sequentially is taking huge time. Here is what I'm thinking: I can create two threads. One will generate images and another will upload images. I will also create a list in which I will keep adding the image paths. Once the list has 100 (just an example) items, I will upload them and re-initialize the list to empty. Now if I wait for all 100 images to be uploaded and then start generating next set of images (because I'm re-initializing the list to empty), then there is no use of having multiple threads. Should I have two lists? Thread1 will generate the first 100 images. Once done, Thread2 will start will start uploading and at the same time the first thread will start generating another set of images to list 2 and so on.. I'm not sure if this approach is efficient or will work. Any suggestions/advise?
Is there a limit for box API? If not, then just create a thread pool of say 10 threads and submit all tasks - 50000 - simultaneously. Let the threading be handled by executor service. To optimize more have 2 thread pools, one of size N where N is number of CPU cores for image generation task which is CPU intensive, and second pool of size 20 or so for image uploads which is IO tasks
@@DefogTechThanks for your response. There is no limit for Box. So how would I manage generation and upload? I mean I need to wait until image(s) has/have been generate. How would I maintain that?
@@puneetja create 2 pools, when submitting the image generation tasks to CPU-pool, pass them a reference (in constructor) of the IO pool. So once the images are generated, within the task, create a new upload image task, and submit it to IO pool. This way you dont have to many any queues or threading or locks.
@@DefogTech Okay and this would be in batches. For example, once 500 images are generated then start uploading them and side by side start generating next set?
Why batch them? Just submit 50000 tasks. Its not a big number. If thread pool size (CPU count) is 10, it will automatically become batch size of 10. All other tasks will be stored in queue. Thats the beauty of it, we dont need to worry about that all.
Exactly, since it had only one thread, what happens if you submit multiple tasks while it is already using that thread to execute a task. It needs to save the tasks somewhere so that it can pick them up later when threas is free
@@tarunkundhiya5196 ah, good point!! Its a single generic implementation (same class for SingleThreadedExecutor vs FixedThreadPool), thus the blocking queue. So for pulling items out of queue there is single thread but I am wondering what about other thread which pushes into the queue (and what if there are multiple threads which submit the tasks), how to coordinate between these conflicts. I am not sure if normal queue will work since one thread is adding and other thread is removing from the queue.
That crow needs a task.
Haha .. I just noticed it now. Maybe the crow is doing its task.
Now I can't ignore cow cow 🙄😭
Savage
He's doing his asynchronous task
Whats amazing is that you make even complex concept interesting and easy to understand :)
Best concurrency/multithreaded insights playlist ever found!!
Excellent. You are touching the complicated areas to understand, but explaining in simple manner. Appreciate.
Thanks much! I'm very happy and motivated that's it helping folks.
Introduction:
- The video discusses the different types of thread pools provided by Java's `ExecutorService`.
Thread Pool Types:
1. Fixed Thread Pool:
- Has a fixed number of threads.
- All submitted tasks are stored in a blocking queue.
- Threads fetch and execute tasks sequentially.
- Ideal for CPU-intensive tasks.
2. Cached Thread Pool:
- Does not have a fixed number of threads.
- Uses a synchronous queue to hold tasks.
- Creates threads as needed and can kill idle threads.
- Suitable for IO-intensive tasks.
3. Scheduled Thread Pool:
- Used for scheduling tasks to run after a certain delay or at fixed intervals.
- Tasks are stored in a delay queue.
- Provides methods for scheduling tasks once, at fixed rates, or with fixed delays.
4. Single Threaded Executor:
- Similar to fixed thread pool but with only one thread.
- Ensures tasks are executed sequentially.
- Useful for tasks that require strict sequential execution.
Summary:
- Java's `ExecutorService` provides various types of thread pools for managing tasks efficiently.
- The choice of thread pool depends on the nature of tasks, such as CPU-intensive or IO-intensive, and the scheduling requirements.
- Understanding the characteristics of each thread pool helps in selecting the appropriate one for different scenarios.
the videos are amazing, the most interesting aspect is you connect cpu cores caches and the threads, hardware to software, great sir, please continue this you are really a guru thanks👍
This is what I call as an Excellent Explanation .. HatsOff !!! 🤟
Honestly you are exceptionally awesome, such an easy explanation style. Hats off
Explanation is very clear. I have understood the Java threads.
Explanation was very good. I have one opinion, I might be wrong here but I don't think SingleThreadedExecutor executes task in sequential manner. It might pick up the tasks in order but it doesn't mean that it will pick it up after completing the first one. Because, in case first thread is doing some IO operation, the CPU and the thread will start processing the next task.
You are right. If one of the I/O task is waiting then single thread can continue to execute other tasks while waiting for the I/O task to complete. FIFO ordering is not guaranteed unless all tasks are CPU based.
Explanation is very good Sir. Is that a crow in background !!! :)
Thank you! Yes, unfortunately it is a crow.. I wasnt paying attention that day... Subsequent videos have better audio :)
lol !!
highly detailed sessions great work.
Amazing explanation, great playlist for clearing concepts!!
Excellent explanation Deepak - request you to continue creating more and more videos covering all the major aspects like Collections - DS etc, Great work keep it up :) And thanks a ton !!
Thanks
I really liked the status bar at the bottom of your video.
Great explanation.. simple and easy to grab and a quick bite to get a very holistic view about the executors framework. I have watched this and some other videos of this series and are really good. Just to add another difference b/w a "fixed-size thread pool" and a "single-threaded thread pool" is that in case of a "fixed-size thread pool", you can still update/re-configure the pool size set in the constructor later on using the setMaximumPoolSize() method of the ThreadPoolExecutor class.
wow awesome explanation
Thank you sir!
Thanks!
I think one thing need to clarify is required between scheduleAtFixedRate and scheduleWithFixedDelay
1) The fixedRate run periodically, even though the last same execution is not finished, the pool will arrange another thread to proceed, so in this case there are two threads execute the same operation.
2) The fixedDelay will wait the last same operation to finish and then delay say 10 seconds to restart the same operation.
God level explanation! Thank you.
Nice Explanation of critical functionalities in simple way. Thank you !
Complicated theory but you explained it very nicely and now it seems easy to implement.Thanks a ton
Crisp clear and precise 👍👍👍
Your narration is very good 👍
brilliant dude...keep the good work going
Done thanks
Types of thread pools
1. Fixed thread pool
2. Cached thread pool
- dynamically creates and terminates threads based on the number of tasks being submitted and wether there are idle threads (frequency of tasks being submitted)
3. Scheduled thread pool
- uses a delay queue to keep the tasks, the tasks are not kept sequentially based on when they are submitted, but instead based on when they need to run
ScheduledExecutorService
.schedule(...) for running one after delay
.scheduleAtFixedRate(...) running every x seconds
.scheduleWithFixedDelay(...) schedule and wait x seconds after task completion
4. Single threaded executor
- to ensure tasks are run in order
Very clear explanation. Keep it up.
hi. which thread pool we configure? Is it tomcat server threads or executor thread pool?
Amazing explanation! I love ur videos.
not sure where to use single Threaded executor. apparently its better to use a loop for the sequential task execution scenario
Thank you for tutorial it's very helpful to understand in detail
Please explain about
Threadpoolexeuter constructor
Min
Max
Blookingqueue
Exception
Up to my knowledge
Thanks in advance
Expecting soon
Hi
For a single threaded executor,do we have another advantage than actually running sequential task on a different core ,since such scenario can be achieved sequentially on the main thread as well
I have understood it to be helping in a non blocking sequential execution of ordered tasks only
Thank you for making this series of videos
Great vid ! Clean , clear & crisp :)
Thank you for the video. It solved all my doubts.
One question in case of newSingleThreadExecutor, there will no multithreading right?
effective and sort video, sir you are dammed good, thanks and good blase you.
Thank you very much!
Can you help me with question.
Create 15 fix threads that will initialise arraylist with 1000 integer.
What is diff with fixthread pool with size 1 and single threaded executors
No difference.. they are the same.
What are some of the use cases in which cached thread pool is used?
What is the difference between a fixedpool of size 1 and a singlethread pool? Technically are they both same?
I am facing an issue, my main program is not terminating after completing the execution, why? My program is same as above.
So singleThreadExecuter is sequential execution without any parallelism right?
What is a better idea - having multiple SingleThreadPools or multiple threads ?
great content brother
Hi Sir..
For newCachedThreadPool you have told theoretically it will create number of thread max upto number of task.
But I have check it will exceeds.
Was singlethreadedexexutor really required I mean I don't see what benefit it provides over traditional method ?
This is brilliant. Thank you for this!
Thank you very much for this clear explanation. I would like a little more detail on "scheduleAtFixedRate" method to clarify that if the run time of the task exceeds the rate period, is that an error or does the task simply start immediately again (I assume the latter).
FixedRate scheduler doesn't check finish times of the job, it will keep triggering new ones. The fixedDelay one will wait Post a job's completion before triggering next one
@@DefogTech Does triggering mean a new thread, or just that it is ready to go immediately when other run finishes?
@@simonbaker9909 Depends on the threadpool size. If size is greater than 1, it will assign a new thread. So both tasks will overlap (will run in parallel)
@@DefogTech Really thanks for taking out time to explain such queries.
Explained very well .thanks
Pretty good video, well explained
Hello Sir, Can you please explain where we are using the blocking queue here for task submission? or is it internally from java that the submitted task will be stored in the blocking queue and will be executed by the scheduler one by one?
For standard executors the blocking queue is managed internally. For custom executors we can create one and supply it from outside
Thank you so much for the explanation!
Thank you for detailed explanation. Although its 3 years old video, but if you get some time, kindly review: Is Delay Queue same as priority queue? Are there in retry options for Single Threaded Executor Service. Suppose the task 1 keeps on failing and we want to retry it just for n number of times and after that Task 2 should be tried.
Hi. Great explanation. Went directly into mind.
One query. There should be some way to stop the given tasks like for ScheduledatFixedRate right. If i want to exexute a task with rate of 10 and it should end after 50 executions.
Lucky that I found this channel
Good work!!!
Please make more videos on critical topics that could help us to code better
Thank you 😊🙏🏻😇
very nice, thanks so much
If you can't control the number of threads used in a cached thread pool how does it figure out the number of threads? Does it assign threads based on the CPU Cores?
It's based on incoming requests (runnable or callable). It will create a thread for each new request if existing ones are busy.
Nice explanation
It is just osm!!!!!
very nice explanation
Great video, that bird in the background is making a lot of noise, looks like it also want to learn what you are teaching 🤣😁
What about WorkStealingPool?
Excellent
Hi, can you also create and post videos covering data structures and big (O) notations please.
Hi could you please provide the video on Transactions Management with complete example and scenarios
How does in the scheduled pool, delay queue submit the task to the actual thread.(It search for free thread/ already every task is assigned to a thread/or something else)?
It's other way around. Threads will keep polling for tasks from delay queue like normal thread pool, but delay queue only shows tasks once their time has expired.
Brilliant
Clear and concise explanation!However,I have some doubts.What is the difference between a Singlethreaded pool and a Fixedpool with single thread? Also does SingleThreaded pool guarantee the execution of tasks in FIFO order?
Both Singlethreaded pool and a Fixedpool with single thread are the same. Internally SingleThreadPool used normal FixedSizedPool.
Yes, thats correct, SingleThreadedPool will ensure FIFO order of execution.
Nice explanation. Thank you.
I would be great if you could share the slides too.
Great work .. but I think the crow was pointing out one mistake its "scheduleWithFixedDelay" not "scheduleAtFixedDelay" as shown in slide
@DefogTech Just a doubt!. Here in tha case of SingleThreadedExecutor, since only one Thread is there, no need of BlockingQueue right? normal Queue is sufficient.
Still need blocking queue, even though there is only 1 queue taking elements, there are 1 or more threads adding elements from outside to ExecutorService.. so it still requires thread safe queue
@@DefogTech Thank You for making such great videos on Threading.
In case of fixed thread pool what will happen exception occurs or the thread gets interrupted, will it impact the assigned pool size
If for any reason a thread dies, threadpool will replace it with another thread.
really like your videos , but I have one question , in case of Single Threaded Executor why thread pool is using blocked queue , if there is only one thread a normal queue will work the same way right ?
Answer for your question is already addressed below
Thank you very much.
Does SingleThreadedExecutor has its own constructor or we've to use fixedThreadPool and assign to it just one single thread?
Generally we always use static factory methods of Executors class. There are some subtle differences between single-threaded and fixed-threadpool of size 1
@@DefogTech Ok, Now I see it. But in Your video there is no code snippet for static factory method for singleThreadedExecutor, only for fixed, cached and scheduled thread pools so I thought there is no such method.
@@nenadg3665 Ah, my bad.
good explanation thanks
You're welcome!
Very nice
What is the use case of a single threaded executor?
As a monitoring thread, or when only single core CPU is available, or when your use-case demands items to be processed in same order as they arrive.
Thank you for the clear explanation.
I have a doubt - isn't the type 4 - "Single threaded executor" same as single threaded application. After all, everything will run in sequence..
yes, but you could have multiple pools in your application right? some can be multi-threaded some can be single-threaded. So at an application you still have many threads running concurrently.
@@DefogTech oh right, Thanks..
@@DefogTech i am still confused that what is the need for single thread executor? Can you please give some specific example.
Really thanks for putting up such high quality stuff. Coming to ScheduledThreadPool, is DelayQueue actually PriorityQueue - prioritizing the tasks based on execution time ??
no the implementation is different for both, so its a distinct data structure
You are amazing !
Awesome!! Thank You
Crows.. sorry .. Bows ..for the explanation :)
I have a scenario where I need to convert around 50000 images to PNG format and then upload them to Box (using Java API's). Performing it sequentially is taking huge time.
Here is what I'm thinking:
I can create two threads. One will generate images and another will upload images. I will also create a list in which I will keep adding the image paths. Once the list has 100 (just an example) items, I will upload them and re-initialize the list to empty. Now if I wait for all 100 images to be uploaded and then start generating next set of images (because I'm re-initializing the list to empty), then there is no use of having multiple threads.
Should I have two lists? Thread1 will generate the first 100 images. Once done, Thread2 will start will start uploading and at the same time the first thread will start generating another set of images to list 2 and so on..
I'm not sure if this approach is efficient or will work. Any suggestions/advise?
Is there a limit for box API?
If not, then just create a thread pool of say 10 threads and submit all tasks - 50000 - simultaneously. Let the threading be handled by executor service.
To optimize more have 2 thread pools, one of size N where N is number of CPU cores for image generation task which is CPU intensive, and second pool of size 20 or so for image uploads which is IO tasks
@@DefogTechThanks for your response. There is no limit for Box. So how would I manage generation and upload? I mean I need to wait until image(s) has/have been generate. How would I maintain that?
@@puneetja create 2 pools, when submitting the image generation tasks to CPU-pool, pass them a reference (in constructor) of the IO pool. So once the images are generated, within the task, create a new upload image task, and submit it to IO pool.
This way you dont have to many any queues or threading or locks.
@@DefogTech Okay and this would be in batches. For example, once 500 images are generated then start uploading them and side by side start generating next set?
Why batch them? Just submit 50000 tasks. Its not a big number. If thread pool size (CPU count) is 10, it will automatically become batch size of 10. All other tasks will be stored in queue.
Thats the beauty of it, we dont need to worry about that all.
Thanks Defog for such a nice explanation . One question why does SingleThreadedExecutor needs a Blocking Queue ? [since there is only one thread]
Exactly, since it had only one thread, what happens if you submit multiple tasks while it is already using that thread to execute a task. It needs to save the tasks somewhere so that it can pick them up later when threas is free
@@DefogTech A queue would definitely be needed. Actually i meant why i blocking one.
@@tarunkundhiya5196 ah, good point!! Its a single generic implementation (same class for SingleThreadedExecutor vs FixedThreadPool), thus the blocking queue. So for pulling items out of queue there is single thread but I am wondering what about other thread which pushes into the queue (and what if there are multiple threads which submit the tasks), how to coordinate between these conflicts. I am not sure if normal queue will work since one thread is adding and other thread is removing from the queue.
lmao 2:10 hope u got that glass cleaned up
perfect
Wow
good video but the crow is very consistent in the background :)
Sorry about that. This is my earlier video. These days I take special care to record only when background noise is less.
Thought it (Crow sound) was bit irritating but your explanation was superb. Thanks for sharing your knowledge :)
rofl "consistent"
My attention was all on the crow at certain point.
Too many crows in your area :P
Am I the only one who is hearing crow sound
no you are not the only one, there are lot of comments about the crow :)