First, let’s outline a frame of reference for multithreading and why we may need to use a thread pool.
A thread is an execution context that can run a set of instructions within a process – aka a running program. Multithreaded programming refers to using threads to execute multiple tasks concurrently. Of course, this paradigm is well supported on the JVM.
Although this brings several advantages, primarily regarding the performance of a program, multithreaded programming can also have disadvantages – such as increased complexity of the code, concurrency issues, unexpected results and adding the overhead of thread creation.
In this article, we’re going to take a closer look at how the latter issue can be mitigated by using thread pools in Java.