You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by li...@apache.org on 2022/11/02 09:11:07 UTC

[tomcat] branch 8.5.x updated: Improve TaskQueue.

This is an automated email from the ASF dual-hosted git repository.

lihan pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new a7cfc66de7 Improve TaskQueue.
a7cfc66de7 is described below

commit a7cfc66de7f45fc905248fc7261b24a6793e5fd5
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 b799e3664c..755658a7de 100644
--- a/java/org/apache/tomcat/util/threads/TaskQueue.java
+++ b/java/org/apache/tomcat/util/threads/TaskQueue.java
@@ -101,15 +101,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 df46be1853..3de34cb387 100644
--- a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java
+++ b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java
@@ -1970,6 +1970,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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org