Introduction: - The video explores advanced topics related to the `ExecutorService` in Java. - It covers parameters for creating thread pools, types of queues, rejection handling, and lifecycle methods. Advanced Topics Covered: 1. Parameters for Creating Thread Pools: - Core pool size: Initial size for the thread pool. - Max pool size: Upper threshold for the thread pool size. - Keepalive time: Time for which idle threads remain alive before being killed. - Work queue: Type of queue used to store tasks. - Thread factory: Factory for creating new threads. - Rejection handler: Policy for handling rejected tasks. 2. Types of Queues: - Linked blocking queue: Unbounded queue for fixed and single-threaded executors. - Synchronous queue: Queue with a single storage slot for cached thread pool. - Delayed work queue: Queue for scheduling tasks based on scheduled time. 3. Rejection Handling Policies: - Abort policy: Throws a runtime exception for rejected tasks. - Discard policy: Silently discards rejected tasks. - Discard oldest policy: Discards the oldest task in the queue for rejected tasks. - Caller runs policy: Asks the caller to execute the rejected task. 4. Lifecycle Methods: - `shutdown()`: Initiates shutdown but does not immediately shut down the executor service. - `isShutdown()`: Checks if shutdown has been initiated. - `isTerminated()`: Checks if shutdown is completed. - `awaitTermination()`: Blocks until all tasks are completed or a timeout occurs. - `shutdownNow()`: Initiates shutdown and returns tasks that were not executed. Summary: - Understanding parameters for creating thread pools helps customize the behavior of the `ExecutorService`. - Different types of queues are used based on the nature of the thread pool and task scheduling requirements. - Rejection handling policies dictate how rejected tasks are managed. - Lifecycle methods help control the shutdown process of the executor service.
Really got clarity helped me in interviews,lots of confusion in understanding thread pool u cleared every thing,one more request from my side we want vedios on nio streams,reactive programming sir pls help us by providing u r vedios u r really adorable
0:30 instantiating the fixed thread pool with newFixedThreadPool calls the ThreadPoolExecutor constructor behind the scenes Core pool size initial number of threads in pool Current pool size can be bigger if new threads created Max pool size Keep alive time if thread not active gets killed and pool decreases Core threads are not killed when idle except if allowCoreThreadTimeOut is true 3:00 pool params for different executors 5:00 queue types for different executors LinkedBlockingQueue can keep increasing, used for fixed and single threaded executors, because the threads are bounded, so the task storage area need to be unbounded so we can keep submitting tasks asynchronously SynchronousQueue is a queue with a single slot, used in CachedThreadPool because threads are unbounded so storage space for tasks can be bounded, new threads will be created to take the tasks being submitted DelayedWorkQueue for scheduledThreadPool ArrayBlockingQueue fixed size, if queue is full new thread is created up to maxPoolSize Continue notes
Thank you. I am not familiar with JavaFX.. I am planning on more videos of Spring Boot, Cloud Foundry and Kotlin. Let me know if you want any other topics.
Great videos, very helpful. I have a question regarding the shutdown aspect of the ExecutorService. The whole idea of the ThreadPool is to reuse the threads without creating new Threads time and again as it is a very expensive operation. Then when do we actually opt for shutdown and recreation of a ExecutorService. Many online tutorials and blogs I have seen, the pattern followed is In a method these sequence of steps are performed -- create an executorservice with fixed threadpool ---> executor.submit all tasks --> executor.shutdown() --> Read the future result when ready (Which is a blocking call). So in this model whenever the method of main thread gets executed, the above sequence of steps are repeated where in we are creating and shutting down the threads repeatedly. This doesn't seem right because it is kind of defeating the purpose right??. I believe the exectorService has to be created only once and that should be reused. Can you let us know what is the right model of instantiating the executorService and reusing the thread. And also when should any kind of shutdown on the executorService be used at all??
Ultimate explanation, just one doubt in case of FixedThreadPool and SingleThreadPool keepalivetime is 0 second which is little bit confusing, should it not be infinite or untill unless main application is up.
Excellent Video, something i was looking forward to. There is some confusion at 5:19. It says Que type is LinkedBlockingQue for fixedThreadPool and singleThreadExecutor. Wouldn't it be better to useArrayBlockIngQue since we now the size before hand and it will no go beyond the mentioned size? Anyone would like to comment ? Would be great if maker of this video can guide me in this matter :)
The size of the threads (which will execute the tasks) is fixed but the pool doesn't know the number of tasks that will be submitted. Also, even if we have that info, the queue stores tasks temporarily only when all threads are busy executing previous tasks. The threadpool does not even know which task will take how long. Thus LinkedBlockingQueue makes more sense
Hello. Thanks for the detailed explanation of the Thread Executor. Already subscribed your channel for upcoming videos. I have one question here - For the ShutdownNow() you have mentioned that it'll complete all the tasks which Threads are running and won't wait for the Queued tasks. But in the Javadoc to shutdownNow() method it's saying - 'Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. These tasks are drained (removed) from the task queue upon return from this method. This method does not wait for actively executing tasks to terminate. ' Please advise here. Thanks in advance.
shutdown() will allow existing tasks to complete and will not accept any new tasks. The method immediately returns though. Thus its recommended to use awaitTermination after using this method. shutdownNow() will interrupt existing tasks running, will return tasks waiting in queue, and not accept any new tasks.
Custom Pool uses ArrayBlocking Queue. This means the Queue size is fixed or rather bounded. If the queue gets full, what is the purpose of creating a new thread? The Task will get rejected right?
in the table where you compare different Queue Types, you mentioned for LinkedBlockingQueue "since queue can never become full, new threads are never created". actually the default capacity of LinkedBlockingQueue is Integer.MAX_VALUE, in theory it can become full. so I don't understand the reason of using LinkedBlockingQueue instead of ArrayBlockingQueue here.
Please note that the keep-alive-time for ScheduledThreadPool is not 60 seconds. For Java 8, it is 0 nano-second. For Java 13, it is 10 millisecond. It is very clear from the source code. //Java 8: public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue()); } //Java 13: public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS, new DelayedWorkQueue()); } where DEFAULT_KEEPALIVE_MILLIS = 10L.
Thanks, your explanation is very clear. By the way, i got one question here. Regarding the shutdown of ExecutorService, we know that it will guarantee that all pending/waiting threads will be executed before the executor is terminated (graceful shutdown). In that case, what happen if lets say, our application got terminated by an unknown process (like process kill)..., will it be guarantee as well (that all pending/waiting threads got executed in the queue / pool) ?
For ScheduledThreadPool, it looks like keepAliveTime is 0 NanoSeconds... public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue()); }
shutDown method will just initiate the shut down (thread won't block). ThreadPool will stop accepting new tasks. awaitTermination blocks the current thread for the duration mentioned (or before if all pending tasks are completed before that time). Thus, generally, shutdown is called first, then we use awaitTermination to allow tasks to complete. These are complementary methods.
ultimate!!! Now let the interviewer ask something about executorservice. GAME ON!!
hell true!!!!
Haha
I have enrolled for course on Udemy for multi threading but this is far better than that... Hats off for detailed information. Keep posting thanks
Your playlist on java concurrency is by far the best out there on youtube
Introduction:
- The video explores advanced topics related to the `ExecutorService` in Java.
- It covers parameters for creating thread pools, types of queues, rejection handling, and lifecycle methods.
Advanced Topics Covered:
1. Parameters for Creating Thread Pools:
- Core pool size: Initial size for the thread pool.
- Max pool size: Upper threshold for the thread pool size.
- Keepalive time: Time for which idle threads remain alive before being killed.
- Work queue: Type of queue used to store tasks.
- Thread factory: Factory for creating new threads.
- Rejection handler: Policy for handling rejected tasks.
2. Types of Queues:
- Linked blocking queue: Unbounded queue for fixed and single-threaded executors.
- Synchronous queue: Queue with a single storage slot for cached thread pool.
- Delayed work queue: Queue for scheduling tasks based on scheduled time.
3. Rejection Handling Policies:
- Abort policy: Throws a runtime exception for rejected tasks.
- Discard policy: Silently discards rejected tasks.
- Discard oldest policy: Discards the oldest task in the queue for rejected tasks.
- Caller runs policy: Asks the caller to execute the rejected task.
4. Lifecycle Methods:
- `shutdown()`: Initiates shutdown but does not immediately shut down the executor service.
- `isShutdown()`: Checks if shutdown has been initiated.
- `isTerminated()`: Checks if shutdown is completed.
- `awaitTermination()`: Blocks until all tasks are completed or a timeout occurs.
- `shutdownNow()`: Initiates shutdown and returns tasks that were not executed.
Summary:
- Understanding parameters for creating thread pools helps customize the behavior of the `ExecutorService`.
- Different types of queues are used based on the nature of the thread pool and task scheduling requirements.
- Rejection handling policies dictate how rejected tasks are managed.
- Lifecycle methods help control the shutdown process of the executor service.
Thanks a lot. Multi-threading concepts are more clear after watching your videos.
Want more playlists like this Sir, you made our life easy
You are the real deal! Who is watching in 2024?
The comparative analysis diagram in 4:15 is awesome...!!
Thanks RUclips for recommending this amazing channel
Great videos as always, thank you so much for all the learning content here
I miss the crow already :
Let's ask him to put a virtual CROW as background music while he is editing :D
Super video! I applauded for ₹40.00 👏
Really got clarity helped me in interviews,lots of confusion in understanding thread pool u cleared every thing,one more request from my side we want vedios on nio streams,reactive programming sir pls help us by providing u r vedios u r really adorable
I definitely plan for reactive programming videos in coming weeks. I'm glad these videos are helping folks
0:30 instantiating the fixed thread pool with newFixedThreadPool calls the ThreadPoolExecutor constructor behind the scenes
Core pool size initial number of threads in pool
Current pool size can be bigger if new threads created
Max pool size
Keep alive time if thread not active gets killed and pool decreases
Core threads are not killed when idle except if allowCoreThreadTimeOut is true
3:00 pool params for different executors
5:00 queue types for different executors
LinkedBlockingQueue can keep increasing, used for fixed and single threaded executors, because the threads are bounded, so the task storage area need to be unbounded so we can keep submitting tasks asynchronously
SynchronousQueue is a queue with a single slot, used in CachedThreadPool because threads are unbounded so storage space for tasks can be bounded, new threads will be created to take the tasks being submitted
DelayedWorkQueue for scheduledThreadPool
ArrayBlockingQueue fixed size, if queue is full new thread is created up to maxPoolSize
Continue notes
Just read all parts. Awesome.....
Excellent.Nice explanation
Thank you so much. Very informative
All the videos are awesome and very deeply explained......Thanks a lot
You're welcome sir! Thank you for the kind words.
Superb explanation, Thanks
Good explanation and really good content. Please make video tutorial on JAVAFX Concurrency if possible. Good going keep it up.
Thank you. I am not familiar with JavaFX.. I am planning on more videos of Spring Boot, Cloud Foundry and Kotlin. Let me know if you want any other topics.
Extraordinary video sir
Very well explained..understood the concept ..please upload videos on restful web services
Thank you! Will try for web services
unique videos bro
Hats off. This is awesome
Amazing explanation, thank you
well explained. thanks!
Awesome job .. so thoroughly explained .. just loved it 🙌 👏 👍
Amazing explanation. thanks a lot.
Simply Awesome Bruh !!!!!!!!!!!!
Really awesome bro.keep doing more
Well explained. Thank you, sir.
LifeCycle methods begin at 10.25
very good explanation!!!
Very good explaination bro.....thnx
You're welcome! Thank you for the kind words.
Wow, amazing video. Just osm man
Is the PPT/notes available for quick revision. If yes please can u share it.
Great videos, very helpful.
I have a question regarding the shutdown aspect of the ExecutorService. The whole idea of the ThreadPool is to reuse the threads without creating new Threads time and again as it is a very expensive operation. Then when do we actually opt for shutdown and recreation of a ExecutorService. Many online tutorials and blogs I have seen, the pattern followed is
In a method these sequence of steps are performed -- create an executorservice with fixed threadpool ---> executor.submit all tasks --> executor.shutdown() --> Read the future result when ready (Which is a blocking call).
So in this model whenever the method of main thread gets executed, the above sequence of steps are repeated where in we are creating and shutting down the threads repeatedly. This doesn't seem right because it is kind of defeating the purpose right??. I believe the exectorService has to be created only once and that should be reused. Can you let us know what is the right model of instantiating the executorService and reusing the thread. And also when should any kind of shutdown on the executorService be used at all??
Ultimate explanation, just one doubt in case of FixedThreadPool and SingleThreadPool keepalivetime is 0 second which is little bit confusing, should it not be infinite or untill unless main application is up.
awesome
Excellent Video, something i was looking forward to.
There is some confusion at 5:19. It says Que type is LinkedBlockingQue for fixedThreadPool and singleThreadExecutor. Wouldn't it be better to useArrayBlockIngQue since we now the size before hand and it will no go beyond the mentioned size?
Anyone would like to comment ? Would be great if maker of this video can guide me in this matter :)
The size of the threads (which will execute the tasks) is fixed but the pool doesn't know the number of tasks that will be submitted. Also, even if we have that info, the queue stores tasks temporarily only when all threads are busy executing previous tasks. The threadpool does not even know which task will take how long.
Thus LinkedBlockingQueue makes more sense
@@DefogTech great explanation, updated my knowledge with yours, thanks again for bringing up such a great peice of work, it's hard to find on Internet
very good explanation, if you have time please publish Java8 features like streams.
Hi, I have a video about Java 8 streams. Please check out the channel
Hello. Thanks for the detailed explanation of the Thread Executor. Already subscribed your channel for upcoming videos.
I have one question here - For the ShutdownNow() you have mentioned that it'll complete all the tasks which Threads are running and won't wait for the Queued tasks. But in the Javadoc to shutdownNow() method it's saying - 'Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. These tasks are drained (removed) from the task queue upon return from this method.
This method does not wait for actively executing tasks to terminate. ' Please advise here.
Thanks in advance.
shutdown() will allow existing tasks to complete and will not accept any new tasks. The method immediately returns though. Thus its recommended to use awaitTermination after using this method.
shutdownNow() will interrupt existing tasks running, will return tasks waiting in queue, and not accept any new tasks.
@@DefogTech Thanks for your clarification. Much appreaciated. :)
Custom Pool uses ArrayBlocking Queue. This means the Queue size is fixed or rather bounded. If the queue gets full, what is the purpose of creating a new thread? The Task will get rejected right?
Thank you very much.
Very very informative
in the table where you compare different Queue Types, you mentioned for LinkedBlockingQueue "since queue can never become full, new threads are never created".
actually the default capacity of LinkedBlockingQueue is Integer.MAX_VALUE, in theory it can become full. so I don't understand the reason of using LinkedBlockingQueue instead of ArrayBlockingQueue here.
8:57 How interesting is that!
Amazing bro
Super..very nice explaination
Thank you sir!
Please note that the keep-alive-time for ScheduledThreadPool is not 60 seconds. For Java 8, it is 0 nano-second. For Java 13, it is 10 millisecond. It is very clear from the source code.
//Java 8:
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
//Java 13:
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
where DEFAULT_KEEPALIVE_MILLIS = 10L.
what is blocking queue?
Can you provide the ppt ?? So that we can quickly refer
Good content
very good explanation and depth knowledge of executor service please make more video on Currency API Thanku sir
Sure. More videos are coming.. did you check out the have concurrency playlist? It has many topics covered
@@DefogTech please send me the link concurrency playlist
Here you go - ruclips.net/p/PLhfHPmPYPPRk6yMrcbfafFGSbE2EPK_A6
Thanks, your explanation is very clear. By the way, i got one question here. Regarding the shutdown of ExecutorService, we know that it will guarantee that all pending/waiting threads will be executed before the executor is terminated (graceful shutdown). In that case, what happen if lets say, our application got terminated by an unknown process (like process kill)..., will it be guarantee as well (that all pending/waiting threads got executed in the queue / pool) ?
If process is killed, all threads will be immediately stopped including the thread pool, thus there is no guarantee that pending jobs will run
how do I tell my program which policy to use ?
There is a constructor which accepts the policy we want to use
For ScheduledThreadPool, it looks like keepAliveTime is 0 NanoSeconds...
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
can you provide with the slides please
Are Rejection policies considered only in case of bounded queues or there is any possibility for Unbounded Queues(LinkedBlockingQueue) also?
Only bounded queues
謝謝!
Thank you for the support! Really appreciate it
Why the maxPool Size of a scheduledThreadPoolExecutor is Integer.MAX_VALUE? Anyone?
What is diff b/w awaitTermination and shut down if both will execute all task in the queue?
shutDown method will just initiate the shut down (thread won't block). ThreadPool will stop accepting new tasks.
awaitTermination blocks the current thread for the duration mentioned (or before if all pending tasks are completed before that time).
Thus, generally, shutdown is called first, then we use awaitTermination to allow tasks to complete. These are complementary methods.
incase "Discard" policy, what will happen to the caller request?
caller will not know.. it will be silently discarded without any exceptions thrown
Can we get PPT's for the lectures?? It would be really very helpful.
Prepare notes.
개념추천
awesome