Author: fhanik Date: Fri Aug 31 13:24:38 2007 New Revision: 571562 URL: http://svn.apache.org/viewvc?rev=571562&view=rev Log: improve the executor, keep a count on active thread, as Executor.getActiveCount loops through all of them each time
Modified: tomcat/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Modified: tomcat/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java?rev=571562&r1=571561&r2=571562&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java Fri Aug 31 13:24:38 2007 @@ -49,6 +49,8 @@ protected String name; + protected AtomicInteger activeCount = new AtomicInteger(0); + private LifecycleSupport lifecycle = new LifecycleSupport(this); // ---------------------------------------------- Constructors public StandardThreadExecutor() { @@ -63,7 +65,15 @@ TaskQueue taskqueue = new TaskQueue(); TaskThreadFactory tf = new TaskThreadFactory(namePrefix); lifecycle.fireLifecycleEvent(START_EVENT, null); - executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf); + executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf) { + protected void beforeExecute(Thread t,Runnable r) { + activeCount.addAndGet(1); + } + + protected void afterExecute(Runnable r,Throwable t) { + activeCount.addAndGet(-1); + } + }; taskqueue.setParent( (ThreadPoolExecutor) executor); lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); } @@ -74,6 +84,7 @@ if ( executor != null ) executor.shutdown(); executor = null; lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); + activeCount.set(0); } public void execute(Runnable command) { @@ -174,7 +185,7 @@ // Statistics from the thread pool public int getActiveCount() { - return (executor != null) ? executor.getActiveCount() : 0; + return activeCount.get(); } public long getCompletedTaskCount() { @@ -225,7 +236,7 @@ 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 (activeCount.get()<(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 Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=571562&r1=571561&r2=571562&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Fri Aug 31 13:24:38 2007 @@ -184,7 +184,10 @@ */ long lastParachuteCheck = System.currentTimeMillis(); - + /** + * Keep track of how many threads are in use + */ + protected AtomicInteger activeSocketProcessors = new AtomicInteger(0); @@ -762,7 +765,7 @@ TaskQueue taskqueue = new TaskQueue(); TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-"); executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf); - taskqueue.setParent( (ThreadPoolExecutor) executor); + taskqueue.setParent( (ThreadPoolExecutor) executor, this); } } else if ( executor == null ) {//avoid two thread pools being created workers = new WorkerStack(maxThreads); @@ -828,7 +831,7 @@ ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor; tpe.shutdown(); TaskQueue queue = (TaskQueue) tpe.getQueue(); - queue.setParent(null); + queue.setParent(null,null); } executor = null; } @@ -1994,6 +1997,7 @@ } public void run() { + NioEndpoint.this.activeSocketProcessors.addAndGet(1); SelectionKey key = null; try { key = socket.getIOChannel().keyFor(socket.getPoller().getSelector()); @@ -2067,7 +2071,7 @@ status = null; //return to cache processorCache.offer(this); - } + NioEndpoint.this.activeSocketProcessors.addAndGet(-1); } } } @@ -2075,6 +2079,7 @@ // ---------------------------------------------- TaskQueue Inner Class public static class TaskQueue extends LinkedBlockingQueue<Runnable> { ThreadPoolExecutor parent = null; + NioEndpoint endpoint = null; public TaskQueue() { super(); @@ -2089,8 +2094,9 @@ } - public void setParent(ThreadPoolExecutor tp) { + public void setParent(ThreadPoolExecutor tp, NioEndpoint ep) { parent = tp; + this.endpoint = ep; } public boolean offer(Runnable o) { @@ -2100,7 +2106,7 @@ 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 (endpoint.activeSocketProcessors.get()<(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 --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]