1 package ro
.ieval
.fonbot
;
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
;
9 import android
.os
.SystemClock
;
12 * Copyright © 2013 Marius Gavrilescu
14 * This file is part of FonBot.
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.
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.
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/>.
31 * Slim alternative to AsyncTask.<p>
33 * Differences from AsyncTask:
35 * <li>No onPreExecute or onPostExecute
36 * <li>No progress support
37 * <li>Can be retried if necessary
40 * @author Marius Gavrilescu <marius@ieval.ro>
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;
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() {
62 retryIsScheduled
= false;
64 }, RETRY_INTERVAL
, TimeUnit
.MILLISECONDS
);
67 synchronized(RETRY_PENDING_TASKS
){
68 for(ExecutableRunnable task
: RETRY_PENDING_TASKS
)
69 EXECUTORS
.execute(task
);
70 RETRY_PENDING_TASKS
.clear();
72 lastRetry
=SystemClock
.elapsedRealtime();
75 /** Execute this <code>ExecutableRunnable</code> */
76 public final void execute(){
78 EXECUTORS
.execute(this);
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);