Table of Contents
In this tutorial, we will learn about Executor’s newFixedThreadPool factory method.
In the last tutorial, I have shared an introduction to ThreadPoolExecutor. If you are not aware of concepts of ThreadPoolExecutor
, you should go through that first.
Executor’s newFixedThreadPool factory method :
This method returns ThreadPoolExecutor
whose maximum size(let’s say n threads) is fixed.If all n threads are busy performing the task and additional tasks are submitted, then they will have to be in the queue until a thread is available.
Syntax:
1 2 3 |
ExecutorService executorService=Executors.newFixedThreadPool(noOfThreads); |
Java newFixedThreadPool example:
Let’s create a very simple example.
Step 1: Create a Runnable task named "LoopTask.java".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; public class LoopTask implements Runnable { private String loopTaskName; public LoopTask(String loopTaskName) { super(); this.loopTaskName = loopTaskName; } @Override public void run() { System.out.println("Starting "+loopTaskName); for (int i = 1; i <= 10; i++) { System.out.println("Executing "+loopTaskName+" with "+Thread.currentThread().getName()+"===="+i); } System.out.println("Ending "+loopTaskName); } } |
Step 2: Create a class named FixedThreadPoolMain.java
. This will be our main class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FixedThreadPoolMain { public static void main(String args[]) { ExecutorService es=Executors.newFixedThreadPool(3); for (int i = 1; i <= 6; i++) { LoopTask loopTask=new LoopTask("LoopTask "+i); es.submit(loopTask); } es.shutdown(); } } |
Let’s run above program to check the output:
Starting LoopTask 3
Starting LoopTask 2
Executing LoopTask 3 with pool-1-thread-3====1
Executing LoopTask 1 with pool-1-thread-1====1
Executing LoopTask 3 with pool-1-thread-3====2
Executing LoopTask 2 with pool-1-thread-2====1
Executing LoopTask 3 with pool-1-thread-3====3
Executing LoopTask 1 with pool-1-thread-1====2
Executing LoopTask 3 with pool-1-thread-3====4
Executing LoopTask 2 with pool-1-thread-2====2
Executing LoopTask 3 with pool-1-thread-3====5
Executing LoopTask 3 with pool-1-thread-3====6
Executing LoopTask 1 with pool-1-thread-1====3
Executing LoopTask 3 with pool-1-thread-3====7
Executing LoopTask 2 with pool-1-thread-2====3
Executing LoopTask 3 with pool-1-thread-3====8
Executing LoopTask 1 with pool-1-thread-1====4
Executing LoopTask 3 with pool-1-thread-3====9
Executing LoopTask 2 with pool-1-thread-2====4
Executing LoopTask 3 with pool-1-thread-3====10
Executing LoopTask 1 with pool-1-thread-1====5
Executing LoopTask 1 with pool-1-thread-1====6
Ending LoopTask 3
Executing LoopTask 2 with pool-1-thread-2====5
Executing LoopTask 2 with pool-1-thread-2====6
Executing LoopTask 2 with pool-1-thread-2====7
Executing LoopTask 2 with pool-1-thread-2====8
Executing LoopTask 2 with pool-1-thread-2====9
Executing LoopTask 1 with pool-1-thread-1====7
Executing LoopTask 2 with pool-1-thread-2====10
Starting LoopTask 4
Ending LoopTask 2
Executing LoopTask 1 with pool-1-thread-1====8
Starting LoopTask 5
Executing LoopTask 4 with pool-1-thread-3====1
Executing LoopTask 5 with pool-1-thread-2====1
Executing LoopTask 1 with pool-1-thread-1====9
Executing LoopTask 1 with pool-1-thread-1====10
Ending LoopTask 1
Executing LoopTask 5 with pool-1-thread-2====2
Executing LoopTask 4 with pool-1-thread-3====2
Executing LoopTask 4 with pool-1-thread-3====3
Executing LoopTask 5 with pool-1-thread-2====3
Starting LoopTask 6
Executing LoopTask 5 with pool-1-thread-2====4
Executing LoopTask 4 with pool-1-thread-3====4
Executing LoopTask 5 with pool-1-thread-2====5
Executing LoopTask 5 with pool-1-thread-2====6
Executing LoopTask 6 with pool-1-thread-1====1
Executing LoopTask 6 with pool-1-thread-1====2
Executing LoopTask 6 with pool-1-thread-1====3
Executing LoopTask 6 with pool-1-thread-1====4
Executing LoopTask 5 with pool-1-thread-2====7
Executing LoopTask 4 with pool-1-thread-3====5
Executing LoopTask 4 with pool-1-thread-3====6
Executing LoopTask 4 with pool-1-thread-3====7
Executing LoopTask 4 with pool-1-thread-3====8
Executing LoopTask 5 with pool-1-thread-2====8
Executing LoopTask 6 with pool-1-thread-1====5
Executing LoopTask 5 with pool-1-thread-2====9
Executing LoopTask 4 with pool-1-thread-3====9
Executing LoopTask 4 with pool-1-thread-3====10
Executing LoopTask 5 with pool-1-thread-2====10
Ending LoopTask 5
Executing LoopTask 6 with pool-1-thread-1====6
Ending LoopTask 4
Executing LoopTask 6 with pool-1-thread-1====7
Executing LoopTask 6 with pool-1-thread-1====8
Executing LoopTask 6 with pool-1-thread-1====9
Executing LoopTask 6 with pool-1-thread-1====10
Ending LoopTask 6
We have used new newFixedThreadPool
, so when we have submitted 6 tasks
, 3 new threads
will be created and will execute 3 tasks
. Other 3 tasks
will wait in wait queue
. As soon as any task will be completed by thread, another task will be picked by this thread and will execute it.
That’s all about Java newFixedThreadPool example.
You may also like: