]> iEval git - fonbot.git/blame - src/ro/ieval/fonbot/ExecutableRunnable.java
Add SH and ROOTSH commands
[fonbot.git] / src / ro / ieval / fonbot / ExecutableRunnable.java
CommitLineData
bf70dcc3
MG
1package ro.ieval.fonbot;
2
3import java.util.LinkedList;
4import java.util.Queue;
bf70dcc3 5import java.util.concurrent.Executors;
e154bdfd
MG
6import java.util.concurrent.ScheduledExecutorService;
7import java.util.concurrent.TimeUnit;
8
9import android.os.SystemClock;
bf70dcc3
MG
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 */
42abstract class ExecutableRunnable implements Runnable {
43 /** Executor used to execute instances of this class */
561cd6dc 44 private static final ScheduledExecutorService EXECUTORS = Executors.newSingleThreadScheduledExecutor();
bf70dcc3 45 /** Queue containing <code>ExecutableRunnable</code>s that should be retried */
561cd6dc 46 private static final Queue<ExecutableRunnable> RETRY_PENDING_TASKS = new LinkedList<ExecutableRunnable>();
e154bdfd 47 /** Minimum interval between task retries, in milliseconds */
561cd6dc 48 private static final long RETRY_INTERVAL = 30000;
e154bdfd
MG
49 /** {@link SystemClock#elapsedRealtime()} time of last successful call to {@link #retryTasks()} */
50 private static long lastRetry = 0;
561cd6dc 51 /** True if a retryTasks run is already scheduled on {@link #EXECUTORS} */
e154bdfd 52 private static volatile boolean retryIsScheduled = false;
bf70dcc3
MG
53
54 /** Run all tasks that should be retried */
55 public static final void retryTasks(){
561cd6dc 56 if(!retryIsScheduled && lastRetry+RETRY_INTERVAL>SystemClock.elapsedRealtime()){
e154bdfd 57 retryIsScheduled = true;
561cd6dc 58 EXECUTORS.schedule(new Runnable() {
e154bdfd
MG
59 @Override
60 public void run() {
61 retryTasks();
62 retryIsScheduled = false;
63 }
561cd6dc 64 }, RETRY_INTERVAL, TimeUnit.MILLISECONDS);
e154bdfd
MG
65 return;
66 }
561cd6dc
MG
67 synchronized(RETRY_PENDING_TASKS){
68 for(ExecutableRunnable task : RETRY_PENDING_TASKS)
69 EXECUTORS.execute(task);
70 RETRY_PENDING_TASKS.clear();
bf70dcc3 71 }
e154bdfd 72 lastRetry=SystemClock.elapsedRealtime();
bf70dcc3
MG
73 }
74
75 /** Execute this <code>ExecutableRunnable</code> */
76 public final void execute(){
77 retryTasks();
561cd6dc 78 EXECUTORS.execute(this);
bf70dcc3
MG
79 }
80
81 /** Mark this <code>ExecutableRunnable</code> as needing to be retried later */
82 protected final void retry(){
561cd6dc
MG
83 synchronized(RETRY_PENDING_TASKS){
84 RETRY_PENDING_TASKS.add(this);
bf70dcc3
MG
85 }
86 }
87}
This page took 0.038596 seconds and 4 git commands to generate.