aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/concurrent
diff options
context:
space:
mode:
authorMarco Jacob <mjacob@union06.de>2012-02-24 12:57:10 +0100
committerMarco Jacob <mjacob@union06.de>2012-02-24 12:57:10 +0100
commita8c07873825e7fd1d800c5ed2a19671fc87ec03e (patch)
tree3de140f0b734f36d66bdd1ab4982ceb9a323c008 /main/src/cgeo/geocaching/concurrent
parent19eff4998cae6cd76f401c546b5b7e0211649728 (diff)
downloadcgeo-a8c07873825e7fd1d800c5ed2a19671fc87ec03e.zip
cgeo-a8c07873825e7fd1d800c5ed2a19671fc87ec03e.tar.gz
cgeo-a8c07873825e7fd1d800c5ed2a19671fc87ec03e.tar.bz2
added concurrent
Diffstat (limited to 'main/src/cgeo/geocaching/concurrent')
-rw-r--r--main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java19
-rw-r--r--main/src/cgeo/geocaching/concurrent/Task.java14
-rw-r--r--main/src/cgeo/geocaching/concurrent/ThreadPool.java83
3 files changed, 116 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java b/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java
new file mode 100644
index 0000000..36bc6f7
--- /dev/null
+++ b/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java
@@ -0,0 +1,19 @@
+package cgeo.geocaching.concurrent;
+
+import java.util.concurrent.ThreadFactory;
+
+public class PriorityThreadFactory implements ThreadFactory {
+ private int priority;
+
+ public PriorityThreadFactory(int priority) {
+ this.priority = priority;
+ }
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread result = new Thread(r);
+ result.setPriority(this.priority);
+ return result;
+ }
+
+}
diff --git a/main/src/cgeo/geocaching/concurrent/Task.java b/main/src/cgeo/geocaching/concurrent/Task.java
new file mode 100644
index 0000000..9fa84e4
--- /dev/null
+++ b/main/src/cgeo/geocaching/concurrent/Task.java
@@ -0,0 +1,14 @@
+package cgeo.geocaching.concurrent;
+
+
+public abstract class Task implements Runnable {
+ private String name = null;
+
+ public Task(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+}
diff --git a/main/src/cgeo/geocaching/concurrent/ThreadPool.java b/main/src/cgeo/geocaching/concurrent/ThreadPool.java
new file mode 100644
index 0000000..b69ec4a
--- /dev/null
+++ b/main/src/cgeo/geocaching/concurrent/ThreadPool.java
@@ -0,0 +1,83 @@
+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;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Util class to handle working Threads.
+ */
+public class ThreadPool {
+ /** The queue holding the Runnable. **/
+ private BlockingQueue<Runnable> queue = null;
+ /** The Executor. **/
+ private ThreadPoolExecutor executor;
+
+ public static final void main(String[] argv) {
+ ThreadPool pool = new ThreadPool(1, new PriorityThreadFactory(Thread.MIN_PRIORITY));
+ for (int i = 0; i < 5; i++) {
+ try {
+ Thread.sleep(6000);
+ } catch (InterruptedException e) {
+ }
+ Task currentTask = new Task("Task-" + i) {
+ public void run() {
+ for (int n = 0; n < 10; n++) {
+ System.out.println(this.getName() + " Log " + n);
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ };
+ try {
+ pool.add(currentTask, 20, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ /**
+ * Creates a ThreadPool with the given maximum amount of parallel Threads running.
+ *
+ * @param maxThreads
+ * Maximum amout of parallel Threads
+ * @param queueSize
+ * Size of the queue taking Runnables before blocking further Runnables
+ * @param timeoutSecs
+ * Timeout for add operations, 0 waits forever
+ */
+ public ThreadPool(int poolSize, ThreadFactory threadFactory) {
+ 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 queue. This will wait for timeoutSecs given in the constructor.
+ * Please no Threads! ThreadPool itself will created or destroy Threads itself
+ *
+ * @param action
+ * The object to run.
+ * @return true/false successful added
+ * @throws InterruptedException
+ * Operation was interrupted
+ * @throws TimeoutException
+ * Timeout occurred
+ */
+ 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;
+ }
+}