ThreadPool Example In Java

The thread pool implementation consists of two parts. A ThreadPool class which is the public interface to the thread pool, and a PoolThread class which implements the threads that execute the tasks.


ThreadPool.Java


    public class ThreadPool 
    {  
    private BlockingQueue taskQueue = null;  
    private List<PoolThread> threads = new ArrayList<PoolThread>();  
    private boolean isStopped = false;  
    public ThreadPool(int noOfThreads, int maxNoOfTasks)
    {  
    taskQueue = new BlockingQueue(maxNoOfTasks);  
    for(int i=0; i<noOfThreads; i++){  
    threads.add(new PoolThread(taskQueue));  
    }  
    for(PoolThread thread : threads){  
    thread.start();  
    }  
    }  
    public void synchronized execute(Runnable task){  
    if(this.isStopped) 
    throw new IllegalStateException("ThreadPool is stopped");  
    this.taskQueue.enqueue(task); 
    }  
    public synchronized void stop()
    { this.isStopped = true;  
    for(PoolThread thread : threads){  
    thread.stop(); 
    }  
    }  
    }


  PoolThread.java


    public class PoolThread extends Thread {
    private BlockingQueue taskQueue = null;
    private boolean isStopped = false;
    public PoolThread(BlockingQueue queue){
    taskQueue = queue;
    }
    public void run(){
    while(!isStopped()){
    try{
    Runnable runnable = (Runnable) taskQueue.dequeue();
    runnable.run();
    } catch(Exception e){
    //log or otherwise report exception,but keep pool thread alive.
    }
    }
    }
    public synchronized void stop(){
    isStopped = true;
    this.interrupt();
    //break pool thread out of dequeue() call.
    }
    public synchronized void isStopped() {
    return isStopped;
    }
    }
To execute a task the method ThreadPool.execute(Runnable r) is called with a Runnable implementation as parameter. The Runnable is enqueued in the blocking queue internally, waiting to be dequeued.
The Runnable will be dequeued by an idle PoolThread and executed. You can see this in the PoolThread.run() method. After execution the PoolThread loops and tries to dequeue a task again, until stopped.
To stop the ThreadPool the method ThreadPool.stop() is called. The stop called is noted internally in the isStopped member. Then each thread in the pool is stopped by calling PoolThread.stop(). Notice how the execute() method will throw an IllegalStateException if execute() is called after stop() has been called.
The threads will stop after finishing any task they are currently executing. Notice the this.interrupt() call in PoolThread.stop(). This makes sure that a thread blocked in a wait() call inside the taskQueue.dequeue() call breaks out of the wait() call, and leaves the dequeue() method call with an InterruptedException thrown. This exception is caught in the PoolThread.run() method, reported, and then the isStopped variable is checked. Since isStopped is now true, the PoolThread.run() will exit and the thread dies.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email