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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
class SomeThread extends Thread{ private String name; public SomeThread(String name) { this.name = name; } @Override public void run() { System.out.println("Starting thread: "+name); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Thread ended: "+name); } } public class App { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); SomeThread thread1 = new SomeThread("Thread 1"); SomeThread thread2 = new SomeThread("Thread 2"); SomeThread thread3 = new SomeThread("Thread 3"); SomeThread thread4 = new SomeThread("Thread 4"); SomeThread thread5 = new SomeThread("Thread 5"); SomeThread thread6 = new SomeThread("Thread 6"); SomeThread thread7 = new SomeThread("Thread 7"); SomeThread thread8 = new SomeThread("Thread 8"); SomeThread thread9 = new SomeThread("Thread 9"); SomeThread thread10 = new SomeThread("Thread 10"); SomeThread thread11 = new SomeThread("Thread 11"); SomeThread thread12 = new SomeThread("Thread 12"); SomeThread thread13 = new SomeThread("Thread 13"); SomeThread thread14 = new SomeThread("Thread 14"); SomeThread thread15 = new SomeThread("Thread 15"); SomeThread thread16 = new SomeThread("Thread 16"); executorService.execute(thread1); executorService.execute(thread2); executorService.execute(thread3); executorService.execute(thread4); executorService.execute(thread5); executorService.execute(thread6); executorService.execute(thread7); executorService.execute(thread8); executorService.execute(thread9); executorService.execute(thread10); executorService.execute(thread11); executorService.execute(thread12); executorService.execute(thread13); executorService.execute(thread14); executorService.execute(thread15); executorService.execute(thread16); executorService.shutdown(); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
Starting thread: Thread 1 Starting thread: Thread 2 Thread ended: Thread 1 Starting thread: Thread 3 Thread ended: Thread 2 Starting thread: Thread 4 Thread ended: Thread 3 Starting thread: Thread 5 Thread ended: Thread 4 Starting thread: Thread 6 Thread ended: Thread 5 Starting thread: Thread 7 Thread ended: Thread 6 Starting thread: Thread 8 Thread ended: Thread 7 Starting thread: Thread 9 Thread ended: Thread 8 Starting thread: Thread 10 Thread ended: Thread 9 Starting thread: Thread 11 Thread ended: Thread 10 Starting thread: Thread 12 Thread ended: Thread 11 Starting thread: Thread 13 Thread ended: Thread 12 Starting thread: Thread 14 Thread ended: Thread 13 Starting thread: Thread 15 Thread ended: Thread 14 Starting thread: Thread 16 Thread ended: Thread 15 Thread ended: Thread 16 |
Contributed by: Poonam Tomar