diff options
| author | Simon Rettberg | 2016-01-05 09:35:04 +0100 |
|---|---|---|
| committer | Simon Rettberg | 2016-01-05 09:35:04 +0100 |
| commit | ab82089aed8329d23e1315a84984061a61ab0ce6 (patch) | |
| tree | d19abbd99115822b0ee4dd4e51f85bad37fe0763 /dozentenmodulserver/src/main/java/org | |
| parent | [client] don't scroll the pane for the changelog to the bottom, always force ... (diff) | |
| download | tutor-module-ab82089aed8329d23e1315a84984061a61ab0ce6.tar.gz tutor-module-ab82089aed8329d23e1315a84984061a61ab0ce6.tar.xz tutor-module-ab82089aed8329d23e1315a84984061a61ab0ce6.zip | |
[server] Allow httpd to handle more than two concurrent connections
Had I read the javadocs properly I would have noticed that the thread
pool only starts growing to its maximum size if the queue is full, not
the other way round. So now we use a modified thread pool borrowed
from SO that does exactly that.
Diffstat (limited to 'dozentenmodulserver/src/main/java/org')
| -rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/GrowingThreadPoolExecutor.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/GrowingThreadPoolExecutor.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/GrowingThreadPoolExecutor.java new file mode 100644 index 00000000..becccf2f --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/GrowingThreadPoolExecutor.java @@ -0,0 +1,60 @@ +package org.openslx.bwlp.sat.util; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Grows to maximum pool size before queueing. See + * http://stackoverflow.com/a/20153234/2043481 + */ +public class GrowingThreadPoolExecutor extends ThreadPoolExecutor { + private int userSpecifiedCorePoolSize; + private int taskCount; + + public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, + TimeUnit unit, BlockingQueue<Runnable> workQueue) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); + userSpecifiedCorePoolSize = corePoolSize; + } + + @Override + public void execute(Runnable runnable) { + synchronized (this) { + taskCount++; + setCorePoolSizeToTaskCountWithinBounds(); + } + super.execute(runnable); + } + + @Override + protected void afterExecute(Runnable runnable, Throwable throwable) { + super.afterExecute(runnable, throwable); + synchronized (this) { + taskCount--; + setCorePoolSizeToTaskCountWithinBounds(); + } + } + + private void setCorePoolSizeToTaskCountWithinBounds() { + int threads = taskCount; + if (threads < userSpecifiedCorePoolSize) + threads = userSpecifiedCorePoolSize; + if (threads > getMaximumPoolSize()) + threads = getMaximumPoolSize(); + super.setCorePoolSize(threads); + } + + public void setCorePoolSize(int corePoolSize) { + synchronized (this) { + userSpecifiedCorePoolSize = corePoolSize; + } + } + + @Override + public int getCorePoolSize() { + synchronized (this) { + return userSpecifiedCorePoolSize; + } + } +}
\ No newline at end of file |
