Site icon

Create single thread pool using executor in java (with example)

What we will discuss in current post?

What is a thread pool ?

Thread pool is collection of threads, which are created to complete certain tasks. We will create a single thread pool using executor framework. The interaction between thread pool and task is as follows:

  1. Create a thread Pool containing single thread.
  2. Create MyTask by implementing Runnable interface.
  3. Create four MyTasks and assign to single thread pool executor.
  4. Thread pool will finish task one after another.
    • All four task will be executed serially. (i.e. 1 to 4)

Applications of a single thread pool executor?

1. Applications in a real world:

  1. We can use the single thread executor, when we would like to execute tasks one after another.
    • E.g. in our day to day activities, some time, we want the tasks to be executed in fixed order.
  2. As shown in Fig 1, we want to execute task in fixed order.
  3. We can use single thread pool executor, to achieve it.
Fig 1: Employee Routine

2. Applications in a enterprise world:

  1. In enterprise applications, some times we have the background jobs, where tasks are to be executed one after another.
    1. e.g. raising some events, then
    2. sending notifications, then
    3. acknowledging a record in a database.
  2. In such a scenarios, we can use single thread pool executor ( to execute tasks serially).

Program: single thread pool using executor framework in java.

package org.learn;

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

class MyTask implements Runnable {
	private String name;

	public MyTask(String name) {
		this.name = name;
	}

	public void run() { // Start of step 4
		System.out.println("Start executing " + name);
		try {
			Thread.sleep(1000);
			System.out.println("Executing " + name);
		} catch (InterruptedException e) {
			// skipping the catch as of now
			e.printStackTrace();
		}
		System.out.println("Finished execution " + name);
		System.out.println();
	} /// End of step 4
}

public class SingleThreadPool {
	public static void main(String[] args) {
		// Step No 1
		ExecutorService executor = Executors.newSingleThreadExecutor();
		for (int number = 0; number < 4; number++) {
			// Step No 2
			Runnable worker = new MyTask("MyTask " + number);
			// Step No 3
			executor.execute(worker);
		}
		executor.shutdown();

		// Waiting for all thread to finish
		while (!executor.isTerminated())
			;
		System.out.println("All threads finished");
	}
}

Output: single thread pool using executor framework in java

Start executing MyTask 0
Executing MyTask 0
Finished execution MyTask 0

Start executing MyTask 1
Executing MyTask 1
Finished execution MyTask 1

Start executing MyTask 2
Executing MyTask 2
Finished execution MyTask 2

Start executing MyTask 3
Executing MyTask 3
Finished execution MyTask 3

All threads finished

Executors.newFixedThreadPool(1) & Executors. newSingleThreadExecutor()

Exit mobile version