Thursday, May 12, 2022

newFixedThreadPool Example In Java

 newFixedThreadPool is the method of Executors class. It is use to create fixed size thread pool.

If you are new in executor fromwork please visit previous article ExecutorService and Thread Pools in java

Java Code Ecample:

package com.javaiq.in;

public class Task implements Runnable {

	private String name;

	public Task(String name) {
		super();
		this.name = name;
	}

	@Override
	public void run() {
		System.out.println("Starting " + name);
		// creating 6 task
		for (int i = 0; i <= 5; i++) {
			System.out.println("Executing " + name + " with " + Thread.currentThread().getName() + " ###### " + i);
		}
		System.out.println("Ending " + name);
	}
}
package com.javaiq.in;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FixedThreadPoolExample {

	public static void main(String args[]) {
		// creating thread pool of size 2
		ExecutorService es = Executors.newFixedThreadPool(2);

		for (int i = 0; i <= 5; i++) {
			Task task = new Task("Task " + i);
			es.submit(task);
		}
		es.shutdown();
	}
}

Output : In the above example we have created 6 task and using newFixedThreadPool to create thread pool of size 2. 

Starting Task 0
Executing Task 0 with pool-1-thread-1 ###### 0
Executing Task 0 with pool-1-thread-1 ###### 1
Executing Task 0 with pool-1-thread-1 ###### 2
Executing Task 0 with pool-1-thread-1 ###### 3
Executing Task 0 with pool-1-thread-1 ###### 4
Executing Task 0 with pool-1-thread-1 ###### 5
Ending Task 0
Starting Task 2
Executing Task 2 with pool-1-thread-1 ###### 0
Executing Task 2 with pool-1-thread-1 ###### 1
Executing Task 2 with pool-1-thread-1 ###### 2
Executing Task 2 with pool-1-thread-1 ###### 3
Executing Task 2 with pool-1-thread-1 ###### 4
Executing Task 2 with pool-1-thread-1 ###### 5
Ending Task 2
Starting Task 3
Executing Task 3 with pool-1-thread-1 ###### 0
Executing Task 3 with pool-1-thread-1 ###### 1
Executing Task 3 with pool-1-thread-1 ###### 2
Executing Task 3 with pool-1-thread-1 ###### 3
Executing Task 3 with pool-1-thread-1 ###### 4
Executing Task 3 with pool-1-thread-1 ###### 5
Ending Task 3
Starting Task 4
Executing Task 4 with pool-1-thread-1 ###### 0
Executing Task 4 with pool-1-thread-1 ###### 1
Executing Task 4 with pool-1-thread-1 ###### 2
Executing Task 4 with pool-1-thread-1 ###### 3
Executing Task 4 with pool-1-thread-1 ###### 4
Executing Task 4 with pool-1-thread-1 ###### 5
Ending Task 4
Starting Task 5
Executing Task 5 with pool-1-thread-1 ###### 0
Executing Task 5 with pool-1-thread-1 ###### 1
Executing Task 5 with pool-1-thread-1 ###### 2
Executing Task 5 with pool-1-thread-1 ###### 3
Executing Task 5 with pool-1-thread-1 ###### 4
Executing Task 5 with pool-1-thread-1 ###### 5
Ending Task 5
Starting Task 1
Executing Task 1 with pool-1-thread-2 ###### 0
Executing Task 1 with pool-1-thread-2 ###### 1
Executing Task 1 with pool-1-thread-2 ###### 2
Executing Task 1 with pool-1-thread-2 ###### 3
Executing Task 1 with pool-1-thread-2 ###### 4
Executing Task 1 with pool-1-thread-2 ###### 5
Ending Task 1

No comments:

Post a Comment