Make sure that we don't need to use any locks or synchronized statements to get our...
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 22 Mar 2007 01:21:25 +0000 (01:21 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 22 Mar 2007 01:21:25 +0000 (01:21 +0000)
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

java/org/apache/catalina/core/StandardThreadExecutor.java

index f0f586d..53a4d92 100644 (file)
@@ -11,6 +11,7 @@ import org.apache.catalina.Executor;
 import org.apache.catalina.LifecycleException;\r
 import org.apache.catalina.LifecycleListener;\r
 import org.apache.catalina.util.LifecycleSupport;\r
+import java.util.concurrent.RejectedExecutionException;\r
 \r
 public class StandardThreadExecutor implements Executor {\r
     \r
@@ -59,8 +60,14 @@ public class StandardThreadExecutor implements Executor {
     }\r
     \r
     public void execute(Runnable command) {\r
-        if ( executor != null ) executor.execute(command);\r
-        else throw new IllegalStateException("StandardThreadPool not started.");\r
+        if ( executor != null ) {\r
+            try {\r
+                executor.execute(command);\r
+            } catch (RejectedExecutionException rx) {\r
+                //there could have been contention around the queue\r
+                if ( !( (TaskQueue) executor.getQueue()).force(command) ) throw new RejectedExecutionException();\r
+            }\r
+        } else throw new IllegalStateException("StandardThreadPool not started.");\r
     }\r
 \r
     public int getThreadPriority() {\r
@@ -171,10 +178,15 @@ public class StandardThreadExecutor implements Executor {
         public void setParent(ThreadPoolExecutor tp) {\r
             parent = tp;\r
         }\r
+        \r
+        public boolean force(Runnable o) {\r
+            if ( parent.isShutdown() ) throw new RejectedExecutionException();\r
+            return super.offer(o); //forces the item onto the queue, to be used if the task is rejected\r
+        }\r
 \r
         public boolean offer(Runnable o) {\r
             if (parent != null && parent.getPoolSize() < parent.getMaximumPoolSize())\r
-                return false; //force creation of new threads\r
+                return false; //force creation of new threads by rejecting the task\r
             else\r
                 return super.offer(o);\r
         }\r