aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/concurrent
diff options
context:
space:
mode:
authorMarco Jacob <mjacob@union06.de>2012-02-24 19:54:44 +0100
committerMarco Jacob <mjacob@union06.de>2012-02-24 19:54:44 +0100
commit5d95529c0e0264f4b7f7b6d4c288def9e4f51b7d (patch)
tree7c72b8e42fd299f5b0c18e90dbf1e594f9f016e5 /main/src/cgeo/geocaching/concurrent
parentbf76f7628c3982bcbe6998776018dbba0fd949fd (diff)
downloadcgeo-5d95529c0e0264f4b7f7b6d4c288def9e4f51b7d.zip
cgeo-5d95529c0e0264f4b7f7b6d4c288def9e4f51b7d.tar.gz
cgeo-5d95529c0e0264f4b7f7b6d4c288def9e4f51b7d.tar.bz2
renamed ThreadPool to BlockingThreadPool
Diffstat (limited to 'main/src/cgeo/geocaching/concurrent')
-rw-r--r--main/src/cgeo/geocaching/concurrent/BlockingThreadPool.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/concurrent/BlockingThreadPool.java b/main/src/cgeo/geocaching/concurrent/BlockingThreadPool.java
new file mode 100644
index 0000000..8e60d44
--- /dev/null
+++ b/main/src/cgeo/geocaching/concurrent/BlockingThreadPool.java
@@ -0,0 +1,57 @@
+package cgeo.geocaching.concurrent;
+
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * BlockingThreadPool restricts the amount of parallel threads executing Runnables.
+ */
+public class BlockingThreadPool {
+ /** The queue holding the Runnable. **/
+ private BlockingQueue<Runnable> queue = null;
+ /** The Executor. **/
+ private ThreadPoolExecutor executor;
+
+ /**
+ * Creates a ThreadPool with a given maximum of parallel threads running.
+ * Idle threads will be stopped until new threads are added.
+ *
+ * @param poolSize
+ * Maximum amout of parallel Threads
+ * @param priority
+ * The Thread priority e.g. Thread.MIN_PRIORITY
+ */
+ public BlockingThreadPool(int poolSize, int priority) {
+ ThreadFactory threadFactory = new PriorityThreadFactory(priority);
+ this.queue = new ArrayBlockingQueue<Runnable>(poolSize, true);
+ this.executor = new ThreadPoolExecutor(0, poolSize, 5, TimeUnit.SECONDS, this.queue);
+ this.executor.setThreadFactory(threadFactory);
+ }
+
+ /**
+ * Add a runnable to the pool. This will start the core threads in the underlying
+ * executor and try to add the Runnable to the pool. This method waits until timeout
+ * if no free thread is available.
+ *
+ * @param task
+ * The Runnable to add to the pool
+ * @param timeout
+ * The timeout to wait for a free thread
+ * @param unit
+ * The timeout unit
+ * @return true/false successful added
+ * @throws InterruptedException
+ * Operation was interrupted
+ */
+ public boolean add(Runnable task, int timeout, TimeUnit unit) throws InterruptedException {
+ this.executor.setCorePoolSize(this.executor.getMaximumPoolSize());
+ this.executor.prestartAllCoreThreads();
+ boolean successfull = this.queue.offer(task, timeout, unit);
+ this.executor.setCorePoolSize(0);
+ return successfull;
+ }
+}