Author: fhanik Date: Tue Dec 9 13:22:38 2008 New Revision: 724897 URL: http://svn.apache.org/viewvc?rev=724897&view=rev Log: Implement an execution that adds the task to the queue if rejected, and allow to try to add for a specified time
Modified: tomcat/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java Modified: tomcat/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java?rev=724897&r1=724896&r2=724897&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java Tue Dec 9 13:22:38 2008 @@ -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 @@ /** * 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 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> 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<Runnable> workQueue) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new RejectHandler()); } @Override @@ -63,11 +66,54 @@ return activeCount.get(); } + /** + * [EMAIL PROTECTED] + */ + @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 <tt>Executor</tt> 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(); + } } - - --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]