[concurrency-interest] Whats up with the ThreadPoolExecutor?

Ravinder Singh ravs at online.no
Mon Aug 22 06:01:31 EDT 2005


I don't know if this is a bug or me just using it wrongly. But I add 
work to the pool, and I add the same Runnable each time. Since I try to 
avoid GC. But it seems that it doesn't process the full queue. If I 
create a new Runnable each time its ok.

It seems that the remaining work (unprocessed) is the amount of maximum 
threads. So it seems taht a new thread is not able to start its first 
work somehow...

Tried to extract the code in this small application:
----------------------------------------------------------------------------------------------------------------------------- 

import org.apache.log4j.BasicConfigurator;
import edu.emory.mathcs.backport.java.util.concurrent.*;

public class WPTest
{
   static ThreadPoolExecutor wp = null;
   static Runnable workInProgress = new Woerker();
   static private int cSt, cWrk;
   static int WORKCOUNT = 1000;


   public static void main(String[] args)
   {
       BasicConfigurator.configure();
       wp = new ThreadPoolExecutor(1, 10, 60, TimeUnit.SECONDS, new 
ArrayBlockingQueue(30));
       wp.setCorePoolSize(0);
       wp.setMaximumPoolSize(30);
       wp.setKeepAliveTime(60000, TimeUnit.MILLISECONDS);

       for (int i = 0; i < WORKCOUNT; i++)
       {
           cWrk++;
           addWork(workInProgress);
       }

       while (cSt != WORKCOUNT)
       {
           System.out.println("Counters: " + ":" + cSt + ":" + cWrk);
           try
           {
               Thread.sleep(1000);
           }
           catch (InterruptedException e)
           {
               e.printStackTrace();
           }
       }
       System.out.println("Done...");
   }


   public static class Woerker implements Runnable
   {
       public void run()
       {
           cSt++;
       }
   }


   public static void addWork(Runnable r)
   {
       boolean addOk = false;
       while (!addOk)
       {
           try
           {
               wp.execute(r);
               addOk = true;
           }
           catch (RejectedExecutionException rx)
           {
               // Ignore, keep trying.
           }
       }
   }
}




More information about the Concurrency-interest mailing list