diff options
4 files changed, 8 insertions, 87 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index e09b93b..4c13e81 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -1,8 +1,7 @@ package cgeo.geocaching; -import cgeo.geocaching.concurrent.PriorityThreadFactory; import cgeo.geocaching.concurrent.Task; -import cgeo.geocaching.concurrent.ThreadPool; +import cgeo.geocaching.concurrent.BlockingThreadPool; import cgeo.geocaching.files.LocalStorage; import cgeo.geocaching.geopoint.GeopointFormatter.Format; @@ -24,7 +23,7 @@ public class StaticMapsProvider { /** In my tests the "no image available" image had 5470 bytes, while "street only" maps had at least 20000 bytes */ private static final int MIN_MAP_IMAGE_BYTES = 6000; /** ThreadPool restricting this to 1 Thread. **/ - private static final ThreadPool pool = new ThreadPool(1, new PriorityThreadFactory(Thread.MIN_PRIORITY)); + private static final BlockingThreadPool pool = new BlockingThreadPool(1, Thread.MIN_PRIORITY); public static File getMapFile(final String geocode, String prefix, final int level, final boolean createDirs) { return LocalStorage.getStorageFile(geocode, "map_" + prefix + level, false, createDirs); diff --git a/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java b/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java index 36bc6f7..76379de 100644 --- a/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java +++ b/main/src/cgeo/geocaching/concurrent/PriorityThreadFactory.java @@ -2,6 +2,9 @@ package cgeo.geocaching.concurrent; import java.util.concurrent.ThreadFactory; +/** + * Helper class for setting Thread priority in ThreadPool. + */ public class PriorityThreadFactory implements ThreadFactory { private int priority; diff --git a/main/src/cgeo/geocaching/concurrent/Task.java b/main/src/cgeo/geocaching/concurrent/Task.java index 9fa84e4..2472538 100644 --- a/main/src/cgeo/geocaching/concurrent/Task.java +++ b/main/src/cgeo/geocaching/concurrent/Task.java @@ -1,6 +1,8 @@ package cgeo.geocaching.concurrent; - +/** + * Basic class for Runnables added to ThreadPool. + */ public abstract class Task implements Runnable { private String name = null; diff --git a/main/src/cgeo/geocaching/concurrent/ThreadPool.java b/main/src/cgeo/geocaching/concurrent/ThreadPool.java deleted file mode 100644 index b69ec4a..0000000 --- a/main/src/cgeo/geocaching/concurrent/ThreadPool.java +++ /dev/null @@ -1,83 +0,0 @@ -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; - } -} |
