ASF Bugzilla – Attachment 25866 Details for
Bug 49730
Race condition in StandardThreadExecutor : requests are sometimes enqueued instead of creating new threads
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch for tomcat 7
patch BZ 49730 tomcat7.txt (text/plain), 4.36 KB, created by
Sylvain Laurent
on 2010-08-09 16:38:47 UTC
(
hide
)
Description:
Patch for tomcat 7
Filename:
MIME Type:
Creator:
Sylvain Laurent
Created:
2010-08-09 16:38:47 UTC
Size:
4.36 KB
patch
obsolete
>Index: java/org/apache/tomcat/util/threads/TaskQueue.java >=================================================================== >--- java/org/apache/tomcat/util/threads/TaskQueue.java (revision 983485) >+++ java/org/apache/tomcat/util/threads/TaskQueue.java (working copy) >@@ -19,8 +19,8 @@ > import java.util.Collection; > import java.util.concurrent.LinkedBlockingQueue; > import java.util.concurrent.RejectedExecutionException; >-import java.util.concurrent.ThreadPoolExecutor; > import java.util.concurrent.TimeUnit; >+import org.apache.tomcat.util.threads.ThreadPoolExecutor; > /** > * As task queue specifically designed to run with a thread pool executor. > * The task queue is optimised to properly utilize threads within >@@ -65,7 +65,7 @@ > //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 >- if (parent.getActiveCount()<(parent.getPoolSize())) return super.offer(o); >+ if (parent.getSubmittedCount()<(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 >Index: java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java >=================================================================== >--- java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java (revision 983485) >+++ java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java (working copy) >@@ -24,7 +24,7 @@ > import java.util.concurrent.atomic.AtomicInteger; > /** > * Same as a java.util.concurrent.ThreadPoolExecutor but implements a much more efficient >- * getActiveCount method, to be used to properly handle the work queue >+ * {@link #getSubmittedCount()} method, to be used to properly handle the work queue. > * If a RejectedExecutionHandler is not specified a default one will be configured > * and that one will always throw a RejectedExecutionException > * @author fhanik >@@ -32,7 +32,13 @@ > */ > public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor { > >- private final AtomicInteger activeCount = new AtomicInteger(0); >+ /** >+ * The number of tasks submitted but not yet finished. This includes tasks >+ * in the queue and tasks that have been handed to a worker thread but the >+ * latter did not start executing the task yet. >+ * This number is always greater or equal to {@link #getActiveCount()}. >+ */ >+ private final AtomicInteger submittedCount = new AtomicInteger(0); > > public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { > super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); >@@ -53,18 +59,12 @@ > > @Override > protected void afterExecute(Runnable r, Throwable t) { >- activeCount.decrementAndGet(); >+ submittedCount.decrementAndGet(); > } > >- @Override >- protected void beforeExecute(Thread t, Runnable r) { >- activeCount.incrementAndGet(); >+ public int getSubmittedCount() { >+ return submittedCount.get(); > } >- >- @Override >- public int getActiveCount() { >- return activeCount.get(); >- } > > /** > * {@inheritDoc} >@@ -88,6 +88,7 @@ > * @throws NullPointerException if command or unit is null > */ > public void execute(Runnable command, long timeout, TimeUnit unit) { >+ submittedCount.incrementAndGet(); > try { > super.execute(command); > } catch (RejectedExecutionException rx) { >@@ -95,13 +96,16 @@ > final TaskQueue queue = (TaskQueue)super.getQueue(); > try { > if (!queue.force(command, timeout, unit)) { >+ submittedCount.decrementAndGet(); > throw new RejectedExecutionException("Queue capacity is full."); > } > } catch (InterruptedException x) { >+ submittedCount.decrementAndGet(); > Thread.interrupted(); > throw new RejectedExecutionException(x); > } > } else { >+ submittedCount.decrementAndGet(); > throw rx; > } >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 49730
:
25865
| 25866