Smarter executor, only create threads if no threads are available
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 14 Apr 2007 01:41:35 +0000 (01:41 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 14 Apr 2007 01:41:35 +0000 (01:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@528735 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/StandardThreadExecutor.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index c2d3836..d8b62ff 100644 (file)
@@ -219,10 +219,17 @@ public class StandardThreadExecutor implements Executor {
         }\r
 \r
         public boolean offer(Runnable o) {\r
-            if (parent != null && parent.getPoolSize() < parent.getMaximumPoolSize())\r
-                return false; //force creation of new threads by rejecting the task\r
-            else\r
-                return super.offer(o);\r
+            //we can't do any checks\r
+            if (parent==null) return super.offer(o);\r
+            //we are maxed out on threads, simply queue the object\r
+            if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);\r
+            //we have idle threads, just add it to the queue\r
+            //this is an approximation, so it could use some tuning\r
+            if (parent.getActiveCount()<(parent.getPoolSize())) return super.offer(o);\r
+            //if we have less threads than maximum force creation of a new thread\r
+            if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;\r
+            //if we reached here, we need to add it to the queue\r
+            return super.offer(o);\r
         }\r
     }\r
 \r
index 8b0c7f3..2e76cad 100644 (file)
@@ -2153,8 +2153,17 @@ public class NioEndpoint {
         }
         
         public boolean offer(Runnable o) {
-            if ( parent != null && parent.getPoolSize()<parent.getMaximumPoolSize() ) return false;//force creation of new threads
-            else return super.offer(o);
+            //we can't do any checks
+            if (parent==null) return super.offer(o);
+            //we are maxed out on threads, simply queue the object
+            if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
+            //we have idle threads, just add it to the queue
+            //this is an approximation, so it could use some tuning
+            if (parent.getActiveCount()<(parent.getPoolSize())) return super.offer(o);
+            //if we have less threads than maximum force creation of a new thread
+            if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;
+            //if we reached here, we need to add it to the queue
+            return super.offer(o);
         }
     }