14.12. Thread pools

Thread pools

  • Eclipse: Oxygen
  • Java: 1.8

This tutorial is about a thread pool that is a software design pattern for achieving concurrency of execution in a computer program. Often, also called a replicated workers or worker-crew model, a thread pool maintains several threads waiting for tasks to be allocated for concurrent execution by the supervising program.

In the following program, we have SomeThread class which extends Thread. Inside the App class, we are creating a fixed thread pool from Executors framework. Then we pass the object of thread type for executing threads.  And shut down the Executor pool.  shutdown() method helps us to finish the execution of all the submitted tasks and terminate the thread pool.

As we see the application is executing only two threads simultaneously when a thread in the pool becomes idle.

Output

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments