Abhishek Choudhary
Abhishek Choudhary

Reputation: 8385

Exactly what a Thread Pool in Java doing?

Thread Pool like any ExecutorServices, we defined a newFixedPool of size 3. Now I have a queue of around 10000 runnable tasks. For executing the above process I have these doubts -

  1. To execute the above process , is the executor will let only 3 threads from the queus of tasks to run in one shot?

  2. The Pool will be carrying 3 Threads , and those 3 threads will only be responsible for executing all the 10000 tasks. If it is correct , how a single thread is running different runnable tasks as finally those tasks are as well threads itself , and in the mid of the running of any of the job/tasks , you can assign new responsibility to the Pool Thread.

Upvotes: 3

Views: 714

Answers (2)

Ashish Sharma
Ashish Sharma

Reputation: 672

Below is customThreadPool in java which accept noofThreads and MaxConcurrentTask. Also it has stop() to stop complete ThreadPool

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


@SuppressWarnings("all")
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 LinkedBlockingQueue(maxNoOfTasks);
        for(int i=0; i<noOfThreads; i++) {    
            threads.add(new PoolThread(taskQueue));  
        }   

        for(PoolThread thread : threads) {    
            thread.start();  
        }  
    }  

    public synchronized void execute(Runnable task) {  

        if(this.isStopped) 
            throw  new IllegalStateException("ThreadPool is stopped");

        this.taskQueue.offer(task); 
    }  

    public synchronized void stop() {  
        this.isStopped = true;  
        for(PoolThread thread : threads)    {     
            thread.stopMe();   
        } 
    }
}


@SuppressWarnings("all")
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.poll();  
                runnable.run();     
            } catch(Exception e)    {    
                //log or otherwise report exception,        //but keep pool thread alive.  

            }    
        } 
    } 

    public synchronized void stopMe()   {  
        isStopped = true;   
        this.interrupt(); 
        //break pool thread out of dequeue() call. 
    }  

    public synchronized boolean isStopped() {  
        return isStopped;  
    }
}

Upvotes: 0

Tim Bender
Tim Bender

Reputation: 20442

  1. Yes, at most only 3 threads will be in the pool at once if in fact you are using Executors.newFixedThreadPool(3)

  2. The 10,000 tasks are not Threads they are simply Runnables. A Thread has to be started via Thread#start to actually create a system thread. Tasks (instances of Runnable) are placed in a BlockingQueue. Threads from the thread pool will poll the BlockingQueue for a task to run. When they complete the task, they return to the queue to get another. If more tasks are added, then they are inserted into the BlockingQueue according to the rules of the implementation of that queue. For most queues this is First-In-First-Out, but a PriorityQueue actually uses a Comparator or natural ordering to sort tasks as they are inserted.

Upvotes: 5

Related Questions