Java : Build your Own Custom ExecutorService(ThreadPool)

Angad Yadav
2 min readDec 26, 2019

--

The Favourite Question of Interviewer is to Design Your own ExecutorService.
Today, In this Post I am going to explain how we can make our own ExecutorService.

What is ExecutorService or ThreadpoolExecutor ?
A Service which allow you to submit your task (Runnable Type Task) which is executed by thread asynchronously. The ExecutorService creates and maintains a reusable pool of threads for executing submitted tasks.

Goals:
1. user can set the size of Pool.
2. user can submit tasks to the ExecutorService.
3. It is the Responsibility of ExecutorService to execute submitted task.

Lets, Start our Design.

MyExecutorService service=MyExecutors.myNewFixedThreadPool(3);

Here, MyExecutorService is an Interface and MyExecutors is a class having static method named myNewFixedThreadPool(int capacity) which is use to define the size of thread pool and returning the object of implemented class of MyExecutorService interface which is MyThreadPool class.

MyExecutorService Interface:

interface MyExecutorService{
void submit(Runnable r);
}

MyThreadPool Class: // Implemented Class of MyExecutorService

class MyThreadPool implements MyExecutorService
{
static int capacity, int currentCapacity;
static LinkedBlockingQueue<Runnable> linkedBlockingQueue;
Execution e;

public MyThreadPool(int capacity){
this.capacity=capacity;
currentCapacity=0;
linkedBlockingQueue=new LinkedBlockingQueue<Runnable>();
e=new Execution();
}

@Override
public void submit(Runnable r){
linkedBlockingQueue.add(r);
e.executeMyMethod();
}
}

MyExecutors Class: // To return the reference of MyExecutorService

class MyExecutors{
int capacity;
static MyExecutorService myNewFixedThreadPool(int capacity){
return new MyThreadPool(capacity);
}}

We will need a Queue which can store Runnable tasks and can handle concurrency too so we are going to use LinkedBlockingQueue to store tasks
and We have to define a task for thread pool where threads can pick the tasks submitted by user from Queue to execute them.

@Override
public void run() {
while(true){
if(MyThreadPool.linkedBlockingQueue.size()!=0){
MyThreadPool.linkedBlockingQueue.poll().run();
}}}

MyThreadPool class have method named submit(Runnable r) which is used to submit the task in Queue after Submitting the task to queue we are calling a method executeMyMethod() of a class Execution where we are creating threads if it less than the capacity(core size) and run the thread for the above run method.

We are using MyTask class to create dummy tasks which print the current thread name and ExecutorServiceCustom class is used as Main class to start this program.
You can change the task behaviour and capacity according to you in ExecutorServiceCustom class.

Working Code :

Thanks!

I would love to hear from you

You can reach me for any query, feedback or just want to have a discussion by the following channels:

LinkedIn

Gmail: akyadav7303@gmail.com

Please feel free to share with your fellow developers.

--

--