Add ExecutableRunnable
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 12 Mar 2013 16:16:34 +0000 (18:16 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 12 Mar 2013 16:16:34 +0000 (18:16 +0200)
ExecutableRunnable is a lightweight replacement for AsyncTask.

It does not support onPreExecute()/onPostExecute() or showing progress,
but it can be ran multiple times and retried if it fails.

src/ro/ieval/fonbot/ExecutableRunnable.java [new file with mode: 0644]

diff --git a/src/ro/ieval/fonbot/ExecutableRunnable.java b/src/ro/ieval/fonbot/ExecutableRunnable.java
new file mode 100644 (file)
index 0000000..642661b
--- /dev/null
@@ -0,0 +1,66 @@
+package ro.ieval.fonbot;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+/*
+ * Copyright © 2013 Marius Gavrilescu
+ * 
+ * This file is part of FonBot.
+ *
+ * FonBot is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * FonBot is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FonBot.  If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+/**
+ * Slim alternative to AsyncTask.<p>
+ *
+ * Differences from AsyncTask:
+ * <ul>
+ * <li>No onPreExecute or onPostExecute
+ * <li>No progress support
+ * <li>Can be retried if necessary
+ * </ul>
+ * 
+ * @author Marius Gavrilescu <marius@ieval.ro>
+ */
+abstract class ExecutableRunnable implements Runnable {
+       /** Executor used to execute instances of this class */
+       private static final Executor executor = Executors.newSingleThreadExecutor();
+       /** Queue containing <code>ExecutableRunnable</code>s that should be retried */
+       private static final Queue<ExecutableRunnable> retryPendingTasks = new LinkedList<ExecutableRunnable>();
+
+       /** Run all tasks that should be retried */
+       public static final void retryTasks(){
+               synchronized(retryPendingTasks){
+                       for(ExecutableRunnable task : retryPendingTasks)
+                               task.execute();
+                       retryPendingTasks.clear();
+               }
+       }
+
+       /** Execute this <code>ExecutableRunnable</code> */
+       public final void execute(){
+               retryTasks();
+               executor.execute(this);
+       }
+
+       /** Mark this <code>ExecutableRunnable</code> as needing to be retried later */
+       protected final void retry(){
+               synchronized(retryPendingTasks){
+                       retryPendingTasks.add(this);
+               }
+       }
+}
This page took 0.010276 seconds and 4 git commands to generate.