This is an automated email from the ASF dual-hosted git repository.
lihan pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new 77e025ffaa Improve TaskQueue.
77e025ffaa is described below
commit 77e025ffaa571e58138b0657d143f6084eb996f5
Author: lihan <[email protected]>
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: [email protected]
For additional commands, e-mail: [email protected]