]> iEval git - fonbot.git/blob - src/ro/ieval/fonbot/ExecutableRunnable.java
Do not crash when takePicture fails
[fonbot.git] / src / ro / ieval / fonbot / ExecutableRunnable.java
1 package ro.ieval.fonbot;
2
3 import java.util.LinkedList;
4 import java.util.Queue;
5 import java.util.concurrent.Executors;
6 import java.util.concurrent.ScheduledExecutorService;
7 import java.util.concurrent.TimeUnit;
8
9 import android.os.SystemClock;
10
11 /*
12 * Copyright © 2013 Marius Gavrilescu
13 *
14 * This file is part of FonBot.
15 *
16 * FonBot is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * FonBot is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
28 */
29
30 /**
31 * Slim alternative to AsyncTask.<p>
32 *
33 * Differences from AsyncTask:
34 * <ul>
35 * <li>No onPreExecute or onPostExecute
36 * <li>No progress support
37 * <li>Can be retried if necessary
38 * </ul>
39 *
40 * @author Marius Gavrilescu <marius@ieval.ro>
41 */
42 abstract class ExecutableRunnable implements Runnable {
43 /** Executor used to execute instances of this class */
44 private static final ScheduledExecutorService EXECUTORS = Executors.newSingleThreadScheduledExecutor();
45 /** Queue containing <code>ExecutableRunnable</code>s that should be retried */
46 private static final Queue<ExecutableRunnable> RETRY_PENDING_TASKS = new LinkedList<ExecutableRunnable>();
47 /** Minimum interval between task retries, in milliseconds */
48 private static final long RETRY_INTERVAL = 30000;
49 /** {@link SystemClock#elapsedRealtime()} time of last successful call to {@link #retryTasks()} */
50 private static long lastRetry = 0;
51 /** True if a retryTasks run is already scheduled on {@link #EXECUTORS} */
52 private static volatile boolean retryIsScheduled = false;
53
54 /** Run all tasks that should be retried */
55 public static final void retryTasks(){
56 if(!retryIsScheduled && lastRetry+RETRY_INTERVAL>SystemClock.elapsedRealtime()){
57 retryIsScheduled = true;
58 EXECUTORS.schedule(new Runnable() {
59 @Override
60 public void run() {
61 retryTasks();
62 retryIsScheduled = false;
63 }
64 }, RETRY_INTERVAL, TimeUnit.MILLISECONDS);
65 return;
66 }
67 synchronized(RETRY_PENDING_TASKS){
68 for(ExecutableRunnable task : RETRY_PENDING_TASKS)
69 EXECUTORS.execute(task);
70 RETRY_PENDING_TASKS.clear();
71 }
72 lastRetry=SystemClock.elapsedRealtime();
73 }
74
75 /** Execute this <code>ExecutableRunnable</code> */
76 public final void execute(){
77 retryTasks();
78 EXECUTORS.execute(this);
79 }
80
81 /** Mark this <code>ExecutableRunnable</code> as needing to be retried later */
82 protected final void retry(){
83 synchronized(RETRY_PENDING_TASKS){
84 RETRY_PENDING_TASKS.add(this);
85 }
86 }
87 }
This page took 0.044301 seconds and 4 git commands to generate.