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;
/**
* 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
*
*/
}
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
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 <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();
+ }
+
+ }