[concurrency-interest] Potential recursive failure ofThreadPoolExecutor.DiscardOldestPolicy?

David Holmes davidcholmes at aapt.net.au
Mon May 11 23:17:18 EDT 2009


Ashwin,

The DiscardOldest policy doesn't try to run rejected tasks, it tries to make
room and re-submit them. But yes, this is done in a recursive manner (there
is no other way to do it directly) and so it is possible that the space made
is consumed by another submitter, and so you could get a StackOverflowError.

I agree the recursive aspect of this could be documented more clearly.

David Holmes
  -----Original Message-----
  From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Ashwin
Jayaprakash
  Sent: Tuesday, 12 May 2009 12:09 PM
  To: concurrency-interest at cs.oswego.edu
  Subject: [concurrency-interest] Potential recursive failure
ofThreadPoolExecutor.DiscardOldestPolicy?


  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/20090512/c50c1202/attachment.html>


More information about the Concurrency-interest mailing list