title: How to code run method in Thread pooling
body: <p>I got very confused by reading Thread Pooling. I learnt the concept, how they actually works.
  But I confused in the part , how to code this.</p>
  
  <p>I searched a lot on the net. Finally I got a blog, that have codes , given below, </p>
  
  <p><strong>CONDITION IS,  NOT TO USE IN-BUILT CLASS</strong></p>
  
  <p><strong>Code 1</strong></p>
  
  <pre><code>public class ThreadPool {
  
    private BlockingQueue taskQueue = null;
    private List&lt;PoolThread&gt; threads = new ArrayList&lt;PoolThread&gt;();
    private boolean isStopped = false;
  
    public ThreadPool(int noOfThreads, int maxNoOfTasks){
      taskQueue = new BlockingQueue(maxNoOfTasks);
  
      for(int i=0; i&lt;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();
      }
    }
  
  }
  </code></pre>
  
  <p><strong>Code 2</strong></p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <p><strong>Code 3:-</strong></p>
  
  <pre><code>public class BlockingQueue {
  
    private List queue = new LinkedList();
    private int  limit = 10;
  
    public BlockingQueue(int limit){
      this.limit = limit;
    }
  
    public synchronized void enqueue(Object item)
    throws InterruptedException  {
      while(this.queue.size() == this.limit) {
        wait();
      }
      if(this.queue.size() == 0) {
        notifyAll();
      }
      this.queue.add(item);
    }
  
    public synchronized Object dequeue()
    throws InterruptedException{
      while(this.queue.size() == 0){
        wait();
      }
      if(this.queue.size() == this.limit){
        notifyAll();
      }
  
      return this.queue.remove(0);
    }    
  }
  </code></pre>
  
  <p>I tried to understand , what this code do.
  But I dont get the flow of this code. Can you help me to understand this code.</p>
  
  <pre><code>Mainly I have problems in **Code 2 :- run method**
  
  Why execute method's argument are of Runnable type?
  
  How input array given to this code??
  </code></pre>
  
  <p>Help me.</p>
  
  <p>Thanks in advance.</p>