From 34732e24fac899f86ea7c03974f0a14169ecb4e1 Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 22 Mar 2007 01:21:25 +0000 Subject: [PATCH] Make sure that we don't need to use any locks or synchronized statements to get our executor to work properly. If the task gets executed, means that the threadpool just reached max threads when we were about to add this one in. a simple race condition that we can take care of easily git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@521068 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/core/StandardThreadExecutor.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/java/org/apache/catalina/core/StandardThreadExecutor.java b/java/org/apache/catalina/core/StandardThreadExecutor.java index f0f586d3b..53a4d9209 100644 --- a/java/org/apache/catalina/core/StandardThreadExecutor.java +++ b/java/org/apache/catalina/core/StandardThreadExecutor.java @@ -11,6 +11,7 @@ import org.apache.catalina.Executor; import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleListener; import org.apache.catalina.util.LifecycleSupport; +import java.util.concurrent.RejectedExecutionException; public class StandardThreadExecutor implements Executor { @@ -59,8 +60,14 @@ public class StandardThreadExecutor implements Executor { } public void execute(Runnable command) { - if ( executor != null ) executor.execute(command); - else throw new IllegalStateException("StandardThreadPool not started."); + if ( executor != null ) { + try { + executor.execute(command); + } catch (RejectedExecutionException rx) { + //there could have been contention around the queue + if ( !( (TaskQueue) executor.getQueue()).force(command) ) throw new RejectedExecutionException(); + } + } else throw new IllegalStateException("StandardThreadPool not started."); } public int getThreadPriority() { @@ -171,10 +178,15 @@ public class StandardThreadExecutor implements Executor { public void setParent(ThreadPoolExecutor tp) { parent = tp; } + + public boolean force(Runnable o) { + if ( parent.isShutdown() ) throw new RejectedExecutionException(); + return super.offer(o); //forces the item onto the queue, to be used if the task is rejected + } public boolean offer(Runnable o) { if (parent != null && parent.getPoolSize() < parent.getMaximumPoolSize()) - return false; //force creation of new threads + return false; //force creation of new threads by rejecting the task else return super.offer(o); } -- 2.11.0