]>
Commit | Line | Data |
---|---|---|
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 executor = Executors.newSingleThreadScheduledExecutor(); | |
45 | /** Queue containing <code>ExecutableRunnable</code>s that should be retried */ | |
46 | private static final Queue<ExecutableRunnable> retryPendingTasks = new LinkedList<ExecutableRunnable>(); | |
47 | /** Minimum interval between task retries, in milliseconds */ | |
48 | private static final long retryInterval = 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 #executor} */ | |
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+retryInterval>SystemClock.elapsedRealtime()){ | |
57 | retryIsScheduled = true; | |
58 | executor.schedule(new Runnable() { | |
59 | @Override | |
60 | public void run() { | |
61 | retryTasks(); | |
62 | retryIsScheduled = false; | |
63 | } | |
64 | }, retryInterval, TimeUnit.MILLISECONDS); | |
65 | return; | |
66 | } | |
67 | synchronized(retryPendingTasks){ | |
68 | for(ExecutableRunnable task : retryPendingTasks) | |
69 | executor.execute(task); | |
70 | retryPendingTasks.clear(); | |
71 | } | |
72 | lastRetry=SystemClock.elapsedRealtime(); | |
73 | } | |
74 | ||
75 | /** Execute this <code>ExecutableRunnable</code> */ | |
76 | public final void execute(){ | |
77 | retryTasks(); | |
78 | executor.execute(this); | |
79 | } | |
80 | ||
81 | /** Mark this <code>ExecutableRunnable</code> as needing to be retried later */ | |
82 | protected final void retry(){ | |
83 | synchronized(retryPendingTasks){ | |
84 | retryPendingTasks.add(this); | |
85 | } | |
86 | } | |
87 | } |