Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Whenever you are building a server application, the demand for creating a new thread arises every time a request arrives. This new request is the new thread that is created. This tutorial would be circulating around the thread pool in Java, depicting its advantages and disadvantages followed by the definition!
The topics discussed in this article are:
As the name suggests, the thread pool in Java is actually a pool of Threads. In a simple sense, it contains a group of worker threads that are waiting for the job to be granted. They are reused in the whole process.
In a Thread Pool, a group of fixed size threads is created. Whenever a task has to be granted, one of the threads is pulled out and assigned that task by the service provider, as soon as the job is completed the thread is returned back to the thread pool. Thread pool is preferably used because active threads consume system resources, when is JVM creates too many threads at the same time, the system could run out of memory. Hence the number of threads to be created has to be limited. Therefore the concept of the thread pool is preferred!
Let us move towards our next segment which states the risks related to the thread pool in Java.
There are a few risks while you are dealing with the thread pool, like;
Now, let us move towards the advantages of the thread pool.
Some of the advantages of using thread pool when programming in Java are:
Now, let us check out the disadvantages of the thread pool.
Some of the disadvantages of using thread pool when programming are:
Now let me introduce to the implementation part of the thread pool. Here it goes!
Implementation of a Thread Pool
Check out the code below to understand the concept of thread pool in Java
Code:
package MyPackage; import java.util.concurrent.LinkedBlockingQueue; public class ThreadPool { private final int nThreads; private final PoolWorker[] threads; private final LinkedBlockingQueue<Runnable> queue; public ThreadPool(int Threads) { this.nThreads = Threads; queue = new LinkedBlockingQueue<Runnable>(); threads = new PoolWorker[Threads]; for (int i = 0; i < nThreads; i++) { threads[i] = new PoolWorker(); threads[i].start(); } } public void execute(Runnable task) { synchronized (queue) { queue.add(task); queue.notify(); } } private class PoolWorker extends Thread { public void run() { Runnable task; while (true) { synchronized (queue) { while (queue.isEmpty()) { try { queue.wait(); } catch (InterruptedException e) { System.out.println("An error occurred while queue is waiting: " + e.getMessage()); } } task = (Runnable)queue.poll(); } // If we don't catch RuntimeException, // the pool could leak threads try { task.run(); } catch (RuntimeException e) { System.out.println("Thread pool is interrupted due to an issue: " + e.getMessage()); } } } } }
This brings us to the end of this ‘Thread Pool in Java’ article. I have covered one of the most fundamental and important topics of Java. I hope you are clear with all that has been shared with you in this article.
Make sure you practice as much as possible and revert your experience.
Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Stay up-to-date with the latest Flutter features and best practices through our Flutter Course.
Got a question for us? Please mention it in the comments section of this ‘Thread Pool in Java’ article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co