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

Martin Buchholz martinrb at google.com
Wed Apr 22 13:50:49 EDT 2009


There were many bug fixes to ThreadPoolExecutor that have not yet been
included in jdk6 update releases.  Have you tried latest jdk7 builds?

Martin

On Wed, Apr 22, 2009 at 10:33, Ashwin Jayaprakash
<ashwin.jayaprakash at gmail.com> wrote:
> 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)
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>



More information about the Concurrency-interest mailing list