From: fhanik Date: Tue, 9 Dec 2008 21:22:38 +0000 (+0000) Subject: Implement an execution that adds the task to the queue if rejected, and allow to... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=c3d808733c3ce37ec86106424ab0ae79d9dbfd5a;p=tomcat7.0 Implement an execution that adds the task to the queue if rejected, and allow to try to add for a specified time git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@724897 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java index 60e3bc641..be0bb7e33 100644 --- a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java +++ b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java @@ -17,6 +17,7 @@ package org.apache.tomcat.util.threads; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -24,6 +25,8 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Same as a java.util.concurrent.ThreadPoolExecutor but implements a much more efficient * getActiveCount method, to be used to properly handle the work queue + * If a RejectedExecutionHandler is not specified a default one will be configured + * and that one will always throw a RejectedExecutionException * @author fhanik * */ @@ -41,11 +44,11 @@ public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, new RejectHandler()); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new RejectHandler()); } @Override @@ -63,11 +66,54 @@ public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor return activeCount.get(); } - public void execute(Runnable command, long timeout, TimeUnit unit) { - + /** + * {@inheritDoc} + */ + @Override + public void execute(Runnable command) { + execute(command,0,TimeUnit.MILLISECONDS); } + /** + * Executes the given command at some time in the future. The command + * may execute in a new thread, in a pooled thread, or in the calling + * thread, at the discretion of the Executor implementation. + * If no threads are available, it will be added to the work queue. + * If the work queue is full, the system will wait for the specified + * time and it throw a RejectedExecutionException if the queue is still full after that. + * + * @param command the runnable task + * @throws RejectedExecutionException if this task cannot be + * accepted for execution - the queue is full + * @throws NullPointerException if command or unit is null + */ + public void execute(Runnable command, long timeout, TimeUnit unit) { + try { + super.execute(command); + } catch (RejectedExecutionException rx) { + if (super.getQueue() instanceof TaskQueue) { + TaskQueue queue = (TaskQueue)super.getQueue(); + try { + if (!queue.force(command, timeout, unit)) { + throw new RejectedExecutionException("Queue capacity is full."); + } + } catch (InterruptedException x) { + Thread.currentThread().interrupted(); + throw new RejectedExecutionException(x); + } + } else { + throw rx; + } + + } + } + static class RejectHandler implements java.util.concurrent.RejectedExecutionHandler { + public void rejectedExecution(Runnable r, java.util.concurrent.ThreadPoolExecutor executor) { + throw new RejectedExecutionException(); + } + + }