CustomThreadPoolExecutor in Java Executor Framework
Executors Manage thread execution. At the top of the executor, hierarchy is the Executor interface, which is used to initiate a thread. ExecutorService Extends Executor and provides methods that manage execution. There are three implementations of ExecutorService: ThreadPoolExecutor, ScheduledThreadPoolExecutor, and ForkJoinPool. java.util.concurrent also defines the Executors utility class, which includes some static methods that simplify the creation of various executors. Related to executors are the Future and Callable interfaces. A Future contains a value that is returned by a thread after it executes. Thus, its value becomes defined “in the future,” when the thread terminates. Callable defines a thread that returns a value. In this article, we are going to learn about Custom ThreadPoolExecutor in java.
First, let us discuss two concepts been aggressively used here namely thread pool and blocking queue.
- ThreadPool is a container in which contains some numbers of threads. These threads are given some tasks. When one thread completes its task next task is given to it. While working in a multi-threading environment it’s not practical to create new individual threads for each new task, because creating a new thread is overhead for the operating system.
- A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, If you try to enqueue items to t and the queue is already full. All operations in the blocking queue are thread-safe.
Also, the important specific methods that are to be implemented are as follows:
Method 1: execute()
This method is contained in the Executor interface. This function executes the given task at some time in the future. It returns nothing hence the return type of this method is void.
Method 2: myNewFixedThreadPool()
This is a factory method of Executors class. It is used to create a fixed number of threads in the thread pool.
- Parameter: int number of threads
- Return type: ExecutorService
- Create an interface in which we will create a execute method. This method will execute the task given to it.
- In the code above generated, we have implemented a runnable interface. We are printing the current name of the thread with a delay of 1000 milliseconds. These are the tasks which we are going to execute.
- MyExecutor class provides a static method myNewFixedThreadPool in which we will pass the number of threads we want to create. This method tells the thread pool that how many threads are going to be there in the thread pool. These threads will execute tasks till all tasks get completed.
- This is the custom thread pool class. This class is the heart of the whole mechanism. It uses two important concepts LinkedBlockingQueue and Execution class. The execution class is explained further. This class receives the thread count from the myNewFixedThreadPool method. All the tasks we submit are stored in the queue. All the threads will fetch the tasks from the queue. We submit the task by using the execute method of the MyExecuorService.
- Execution class performs the very important task of adding creating the number of threads that we want in our thread pool. This class is where we are defining how to fetch the task from LinkedBlockingQueue.
- Finally, in this class, we are gathering all the pieces together and our custom thread pool is ready.
Implementation: Here we are passing some threads as 3. The number of tasks is 20 and executing them by using execute method.
Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1 Current Thread :-> Thread-2 Current Thread :-> Thread-0 Current Thread :-> Thread-1
Note: In the above output, we have printed the thread name as defined in the runnable 20 times as we have submitted 20 tasks which is visually described through a video below
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.