[concurrency-interest] Potential recursive failure of ThreadPoolExecutor.DiscardOldestPolicy?

Ashwin Jayaprakash ashwin.jayaprakash at gmail.com
Mon May 11 22:08:54 EDT 2009


I was looking at the source of ThreadPoolExecutor.DiscardOldestPolicy which
seems to have a rather naive way of running the rejected tasks - at least it
seems to me like that.

If the incoming jobs arrive at a faster rate than the rate at which the job
queue gets cleared, won't the rejected task policy potentially cause a
recursive stack overflow? If so, shouldn't the documentation warn us about
such things?

DiscardOldestPolicy code snippet:
    public static class DiscardOldestPolicy implements
RejectedExecutionHandler {
     .. .. .. ..
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }
        }
    }


ThreadPoolExecutor code snippet:
    public void execute(Runnable command) {
        .. .. ....
        for (;;) {
            if (runState != RUNNING) {
                reject(command);
                return;
            }
            if (poolSize < corePoolSize && addIfUnderCorePoolSize(command))
                return;
            if (workQueue.offer(command))
                return;
            Runnable r = addIfUnderMaximumPoolSize(command);
            if (r == command)
                return;
            if (r == null) {
                reject(command);
                return;
            }
            // else retry
        }
    }

Ashwin.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20090511/cea54174/attachment-0001.html>


More information about the Concurrency-interest mailing list