What is ThreadPoolExecutor?
This class is an implementation of the ExecutorService interface via AbstractExecutorService. This class executes tasks of type either Callable or Runnable using threads and threads that are already created and managed through ThreadPool.
What is ThreadPool?
ThreadPool: A pool of worker-threads is waiting for some task to be assigned any time. So, we can concurrently execute tasks without the extra overhead of thread creation. ThreadPool is like an application performance booster. Instead of, Thread creation on every new task/request, threads are taken from manage ThreadPool.
If we don’t use ThreadPool then the Problem we could face:
Soon run out of resources because don’t control thread creation.
In every thread creation, we need several resources from the operating system. Threads are mapped to system-level threads which are actually, operating system resources consider as a significant amount of memory.
For creating Thread-Pool, Java and Spring have their implementations:
1. Java class: ThreadPoolExecutor
2. Spring class: ThreadPoolTaskExecutor
The advantage of using ThreadPool is:
- The thread pool helps to save resources in multi-threaded environment applications.
- The time overhead of thread creation saves and each thread spends more time doing actual work.
- The tasks can be collected in a queue which later fetches whenever a thread is free to execute the tasks and so on.
All the above points help in increasing the performance of an application.