-
Notifications
You must be signed in to change notification settings - Fork 13
Executors newFixedThreadPool Method Example
In this tutorial, we will learn about the 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.
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to the shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
Parameters: nThreads the number of threads in the pool
Returns: the newly created thread pool
Throws: IllegalArgumentException - if nThreads <= 0
Syntax:
ExecutorService executorService = Executors.newFixedThreadPool(noOfThreads);
Let’s create a very simple example.
Step 1: Create a Runnable task named “Task.java”.
class Task implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("[" + Thread.currentThread().getName() + "] " + "Message " + i);
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
}
Step 2: Lets create newFixedThreadPool() method and execute 5 tasks with two threads.
public class FixedThreadPoolExample {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Thread main started");
final ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.shutdown();
System.out.println("Thread main finished");
}
}
Output:
Thread main started
[pool-1-thread-1] Message 0
[pool-1-thread-2] Message 0
Thread main finished
[pool-1-thread-1] Message 1
[pool-1-thread-2] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-2] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-2] Message 3
[pool-1-thread-2] Message 4
[pool-1-thread-1] Message 4
[pool-1-thread-1] Message 0
[pool-1-thread-2] Message 0
[pool-1-thread-1] Message 1
[pool-1-thread-2] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-2] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-2] Message 3
[pool-1-thread-1] Message 4
[pool-1-thread-2] Message 4
[pool-1-thread-1] Message 0
[pool-1-thread-1] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-1] Message 4
We have used new newFixedThreadPool, so when we have submitted 5 task, 2 new threads will be created and will execute 2 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.