Java Executors.newFixedThreadPool Example

By Arvind Rai, May 27, 2022
On this page we will learn using Executors.newFixedThreadPool method.
1. The newFixedThreadPool creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.
2. The newFixedThreadPool has following method declarations.
a. With number of threads.
static ExecutorService newFixedThreadPool(int nThreads) 
The nThreads parameter is the number of threads in the pool.
It returns newly created thread pool.
If nThreads <= 0, It throws IllegalArgumentException.

b. With number of threads and thread factory.
static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) 
The nThreads parameter is the number of threads in the pool.
The threadFactory parameter is the factory to use when creating new threads.
It returns newly created thread pool.
If nThreads <= 0, it throws IllegalArgumentException.

3. At any point, at most nThreads will be active in thread pool.
4. If submitted tasks are more than number of nThreads, they will wait in the queue until a thread is available.
5. If any thread terminates due to a failure during execution prior to shutdown, a new thread is created to take its place if needed to execute subsequent tasks.
6. The threads in the pool will exist until it is explicitly shutdown.

Example-1

In this example we will use newFixedThreadPool with given thread numbers. It uses default thread factory to create threads.
Look into the code.
ExecutorService execService = Executors.newFixedThreadPool(2); 
The thread pool will contain maximum two threads.
Find the example with Runnable.
ExecutorDemo1.java
package com.concretepage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ExecutorDemo1 {
  public static void main(final String... args) throws InterruptedException {
	Runnable task1 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-1");
	  }
	};
	Runnable task2 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-2");
	  }
	};

	ExecutorService execService = Executors.newFixedThreadPool(2);
	execService.submit(task1);
	execService.submit(task2);
	execService.awaitTermination(5, TimeUnit.SECONDS);
	execService.shutdownNow();
  }
} 
Output
Executing Task-2
Executing Task-2
Executing Task-2
Executing Task-1
Executing Task-1
Executing Task-1 

Example-2

Find the newFixedThreadPool example with Callable.
ExecutorDemo2.java
package com.concretepage;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ExecutorDemo2 {
  public static void main(final String... args) throws InterruptedException, ExecutionException {
	Callable<String> task1 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-1");
	  }
	  return "Task-1 Finish.";
	};
	Callable<String> task2 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-2");
	  }
	  return "Task-2 Finish.";
	};

	ExecutorService execService = Executors.newFixedThreadPool(2);
	Future<String> future1 = execService.submit(task1);
	Future<String> future2 = execService.submit(task2);

	String output1 = future1.get();
	System.out.println(output1);

	String output2 = future2.get();
	System.out.println(output2);

	execService.awaitTermination(5, TimeUnit.SECONDS);
	execService.shutdownNow();
  }
} 
Output
Executing Task-2
Executing Task-2
Executing Task-2
Executing Task-1
Executing Task-1
Executing Task-1
Task-1 Finish.
Task-2 Finish. 

Example-3

In this example we will use newFixedThreadPool with given ThreadFactory.
Look into the code.
ExecutorService execService = Executors.newFixedThreadPool(2, threadFactory); 
The thread pool will contain maximum two threads created by given ThreadFactory.
Find the example.
ExecutorDemo3.java
package com.concretepage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class ExecutorDemo3 {
  public static void main(final String... args) throws InterruptedException {
	Runnable task1 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-1");
	  }
	};
	Runnable task2 = () -> {
	  for (int i = 0; i < 3; i++) {
		System.out.println("Executing Task-2");
	  }
	};

	ThreadFactory threadFactory = new MaxPriorityThreadFactory();
	ExecutorService execService = Executors.newFixedThreadPool(2, threadFactory);
	execService.submit(task1);
	execService.submit(task2);
	execService.awaitTermination(5, TimeUnit.SECONDS);
	execService.shutdownNow();
  }
}

class MaxPriorityThreadFactory implements ThreadFactory {
  @Override
  public Thread newThread(Runnable r) {
	Thread t = new Thread(r);
	t.setPriority(Thread.MAX_PRIORITY);
	return t;
  }
} 
Output
Executing Task-1
Executing Task-1
Executing Task-1
Executing Task-2
Executing Task-2
Executing Task-2 

Reference

Class Executors
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us