[concurrency-interest] java.util.concurrent.ThreadPoolExecutor does not execute jobs in some cases

Ashwin Jayaprakash ashwin.jayaprakash at gmail.com
Wed Apr 22 13:33:18 EDT 2009


Hi, I've raised a bug in the Sun Bug Database. It's still under review. But
here it is:

java.util.concurrent.ThreadPoolExecutor does not execute jobs in some cases.

If the corePoolSize if less than the maximumPoolSize, then the thread pool
just does not execute the submitted jobs. The jobs just sit there.

If the corePoolSize is set to 1 instead of 0, then only 1 thread executes
the jobs sequentially.

==================================================
When the corePoolSize is 0:
==================================================

Submitting job 1
Submitting job 2
Submitting job 3
Submitting job 4
Shutting down...
Waiting for job to complete.

(Program never exits)


==================================================
When the corePoolSize is 1:
==================================================
Submitting job 1
Submitting job 2
Submitting job 3
Submitting job 4
Shutting down...
Waiting for job to complete.
Starting job: temp.Temp$StuckJob at 140c281
Finished job: temp.Temp$StuckJob at 140c281
Waiting for job to complete.
Starting job: temp.Temp$StuckJob at a1d1f4
Finished job: temp.Temp$StuckJob at a1d1f4
Waiting for job to complete.
Starting job: temp.Temp$StuckJob at 1df280b
Finished job: temp.Temp$StuckJob at 1df280b
Waiting for job to complete.
Starting job: temp.Temp$StuckJob at 1be0f0a
Finished job: temp.Temp$StuckJob at 1be0f0a
Shut down completed.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Temp {
   public static void main(String[] args) throws ExecutionException,
InterruptedException {
       ThreadPoolExecutor tpe = new ThreadPoolExecutor(0, 512,
               3 * 60, TimeUnit.SECONDS,
               new LinkedBlockingQueue<Runnable>(),
               new SimpleThreadFactory("test"));

       LinkedList<Future> futures = new LinkedList<Future>();

       System.out.println("Submitting job 1");
       futures.add(tpe.submit(new StuckJob()));

       System.out.println("Submitting job 2");
       futures.add(tpe.submit(new StuckJob()));

       System.out.println("Submitting job 3");
       futures.add(tpe.submit(new StuckJob()));

       System.out.println("Submitting job 4");
       futures.add(tpe.submit(new StuckJob()));

       System.out.println("Shutting down...");

       for (Future future : futures) {
           System.out.println("Waiting for job to complete.");
           future.get();
       }

       tpe.shutdown();
       System.out.println("Shut down completed.");
   }

   public static class StuckJob implements Runnable {
       public void run() {
           try {
               System.out.println("Starting job: " + this);
               Thread.sleep(5000);
               System.out.println("Finished job: " + this);
           }
           catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
   }
}

---------- END SOURCE ----------

Workaround:
Set the corePoolSize to be equal to the maximumPoolSize. But this is scary
because if the pool ever reaches its max limit then all those threads will
just sit there instead of shutting down after the idle time out.

Ashwin (http://javaforu.blogspot.com)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20090422/8f7ac5e9/attachment.html>


More information about the Concurrency-interest mailing list