This is an automated email from the ASF dual-hosted git repository. lihan pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new c88e4ca0d8 Improve TaskQueue. c88e4ca0d8 is described below commit c88e4ca0d85d0dd48659b8d1818ee9f9775193e4 Author: lihan <li...@apache.org> AuthorDate: Wed Nov 2 17:01:29 2022 +0800 Improve TaskQueue. Reduce the performance overhead of unnecessary locks by customizing a lock-free method for obtaining pool size --- java/org/apache/tomcat/util/threads/TaskQueue.java | 6 +++--- java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/java/org/apache/tomcat/util/threads/TaskQueue.java b/java/org/apache/tomcat/util/threads/TaskQueue.java index 7104280027..7ff4e3e586 100644 --- a/java/org/apache/tomcat/util/threads/TaskQueue.java +++ b/java/org/apache/tomcat/util/threads/TaskQueue.java @@ -77,15 +77,15 @@ public class TaskQueue extends LinkedBlockingQueue<Runnable> { return super.offer(o); } //we are maxed out on threads, simply queue the object - if (parent.getPoolSize() == parent.getMaximumPoolSize()) { + if (parent.getPoolSizeNoLock() == parent.getMaximumPoolSize()) { return super.offer(o); } //we have idle threads, just add it to the queue - if (parent.getSubmittedCount()<=(parent.getPoolSize())) { + if (parent.getSubmittedCount() <= parent.getPoolSizeNoLock()) { return super.offer(o); } //if we have less threads than maximum force creation of a new thread - if (parent.getPoolSize()<parent.getMaximumPoolSize()) { + if (parent.getPoolSizeNoLock() < parent.getMaximumPoolSize()) { return false; } //if we reached here, we need to add it to the queue diff --git a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java index c660a3ed23..1844dcba28 100644 --- a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java +++ b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java @@ -1941,6 +1941,18 @@ public class ThreadPoolExecutor extends AbstractExecutorService { } } + /** + * Returns the current number of threads in the pool. + * <br><b>NOTE</b>: this method only used in {@link TaskQueue#offer(Runnable)}, + * where operations are frequent, can greatly reduce unnecessary + * performance overhead by a lock-free way. + * @return the number of threads + */ + protected int getPoolSizeNoLock() { + return runStateAtLeast(ctl.get(), TIDYING) ? 0 + : workers.size(); + } + /** * Returns the approximate number of threads that are actively * executing tasks. --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org