Рет қаралды 3,460
This video talks about the basics of ExecutorService. It explains the importance of thread pools in java. Executor framework provides components to manage worker threads.
You can create a thread pool by calling one of the factory methods in Executors class
newFixedThreadPool - Fixed number of threads
newCachedThreadPool - Thread pool size is not limited
newSingleThreadExecutor - A Single thread in the pool
newScheduledThreadPool - Run tasks at regular intervals
In this video we talk in detail about newFixedThreadPool executor.
As the name suggests the thread pool size is fixed.
The corePoolSize and the maximumPoolSize is the same
The number of threads that can be created is dictated by the maximum pool size.
It is backed by a LinkedBlockingQueue - unbounded in size & tasks are executed in FIFO order
If all the worker threads are busy processing tasks, then any new tasks that come in are placed in a queue. Once a worker thread is free, it grabs the tasks from the queue
If a thread dies before shutdown because of some exception, then a new thread takes it place and starts serving new tasks.
Fixed thread pool is a good choice when you want to limit the number of concurrent task
Executor Lifecycle:
ExecutorService can be in three states
Running
Shutting Down
Terminated
Some of the important Lifecycle management methods of ExecutorService
shutdown() - allows graceful shutdown
shutdownNow() - abrupt shutdown
isShutdown()
isTerminated()
awaitTermination(long timeout, TimeUnit unit)
For introduction to multithreading in Java, please look into my previous videos
• Java Multithreading | ...
• Java Multithreading | ...
#Java #Multithreading #ExecutorService #newFixedThreadPool