This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBackgroundExecutor.java
More file actions
486 lines (448 loc) · 15.6 KB
/
Copy pathBackgroundExecutor.java
File metadata and controls
486 lines (448 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2020 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
public final class BackgroundExecutor {
private static final String TAG = "BackgroundExecutor";
public static final Executor DEFAULT_EXECUTOR = Executors.newScheduledThreadPool(2 * Runtime.getRuntime().availableProcessors());
private static Executor executor = DEFAULT_EXECUTOR;
/**
* The default invocation handler for wrong thread execution. It just throws
* {@link IllegalStateException} with explanation what is going wrong.
*
* @see #setWrongThreadListener(BackgroundExecutor.WrongThreadListener)
* @see org.androidannotations.annotations.SupposeBackground
* @see org.androidannotations.annotations.SupposeUiThread
*/
public static final WrongThreadListener DEFAULT_WRONG_THREAD_LISTENER = new WrongThreadListener() {
@Override
public void onUiExpected() {
throw new IllegalStateException("Method invocation is expected from the UI thread");
}
@Override
public void onBgExpected(String... expectedSerials) {
if (expectedSerials.length == 0) {
throw new IllegalStateException("Method invocation is expected from a background thread, but it was called from the UI thread");
}
throw new IllegalStateException("Method invocation is expected from one of serials " + Arrays.toString(expectedSerials) + ", but it was called from the UI thread");
}
@Override
public void onWrongBgSerial(String currentSerial, String... expectedSerials) {
if (currentSerial == null) {
currentSerial = "anonymous";
}
throw new IllegalStateException("Method invocation is expected from one of serials " + Arrays.toString(expectedSerials) + ", but it was called from " + currentSerial + " serial");
}
};
private static WrongThreadListener wrongThreadListener = DEFAULT_WRONG_THREAD_LISTENER;
private static final List<Task> TASKS = new ArrayList<>();
private static final ThreadLocal<String> CURRENT_SERIAL = new ThreadLocal<>();
private BackgroundExecutor() {
}
/**
* Execute a runnable after the given delay.
*
* @param runnable
* the task to execute
* @param delay
* the time from now to delay execution, in milliseconds
*
* if <code>delay</code> is strictly positive and the current
* executor does not support scheduling (if
* {@link #setExecutor(Executor)} has been called with such an
* executor)
* @return Future associated to the running task
*
* @throws IllegalArgumentException
* if the current executor set by {@link #setExecutor(Executor)}
* does not support scheduling
*/
private static Future<?> directExecute(Runnable runnable, long delay) {
Future<?> future = null;
if (delay > 0) {
/* no serial, but a delay: schedule the task */
if (!(executor instanceof ScheduledExecutorService)) {
throw new IllegalArgumentException("The executor set does not support scheduling");
}
ScheduledExecutorService scheduledExecutorService = (ScheduledExecutorService) executor;
future = scheduledExecutorService.schedule(runnable, delay, TimeUnit.MILLISECONDS);
} else {
if (executor instanceof ExecutorService) {
ExecutorService executorService = (ExecutorService) executor;
future = executorService.submit(runnable);
} else {
/* non-cancellable task */
executor.execute(runnable);
}
}
return future;
}
/**
* Execute a task after (at least) its delay <strong>and</strong> after all
* tasks added with the same non-null <code>serial</code> (if any) have
* completed execution.
*
* @param task
* the task to execute
* @throws IllegalArgumentException
* if <code>task.delay</code> is strictly positive and the current
* executor does not support scheduling (if
* {@link #setExecutor(Executor)} has been called with such an
* executor)
*/
public static synchronized void execute(Task task) {
if (task.id != null || task.serial != null) {
/* keep task */
TASKS.add(task);
}
if (task.serial == null || !hasSerialRunning(task.serial)) {
task.executionAsked = true;
task.future = directExecute(task, task.remainingDelay);
}
}
/**
* Execute a task.
*
* @param runnable
* the task to execute
* @param id
* identifier used for task cancellation
* @param delay
* the time from now to delay execution, in milliseconds
* @param serial
* the serial queue (<code>null</code> or <code>""</code> for no
* serial execution)
* @throws IllegalArgumentException
* if <code>delay</code> is strictly positive and the current
* executor does not support scheduling (if
* {@link #setExecutor(Executor)} has been called with such an
* executor)
*/
public static void execute(final Runnable runnable, String id, long delay, String serial) {
execute(new Task(id, delay, serial) {
@Override
public void execute() {
runnable.run();
}
});
}
/**
* Execute a task after the given delay.
*
* @param runnable
* the task to execute
* @param delay
* the time from now to delay execution, in milliseconds
* @throws IllegalArgumentException
* if <code>delay</code> is strictly positive and the current
* executor does not support scheduling (if
* {@link #setExecutor(Executor)} has been called with such an
* executor)
*/
public static void execute(Runnable runnable, long delay) {
directExecute(runnable, delay);
}
/**
* Execute a task.
*
* @param runnable
* the task to execute
*/
public static void execute(Runnable runnable) {
directExecute(runnable, 0);
}
/**
* Execute a task after all tasks added with the same non-null
* <code>serial</code> (if any) have completed execution.
*
* Equivalent to {@link #execute(Runnable, String, long, String)
* execute(runnable, id, 0, serial)}.
*
* @param runnable
* the task to execute
* @param id
* identifier used for task cancellation
* @param serial
* the serial queue to use (<code>null</code> or <code>""</code> for
* no serial execution)
*/
public static void execute(Runnable runnable, String id, String serial) {
execute(runnable, id, 0, serial);
}
/**
* Change the executor.
*
* Note that if the given executor is not a {@link ScheduledExecutorService}
* then executing a task after a delay will not be supported anymore. If it is
* not even a {@link ExecutorService} then tasks will not be cancellable
* anymore.
*
* @param executor
* the new executor
*/
public static void setExecutor(Executor executor) {
BackgroundExecutor.executor = executor;
}
/**
* Changes the default {@link WrongThreadListener}. To restore the default one
* use {@link #DEFAULT_WRONG_THREAD_LISTENER}.
*
* @param listener
* the new {@link WrongThreadListener}
*/
public static void setWrongThreadListener(WrongThreadListener listener) {
wrongThreadListener = listener;
}
/**
* Cancel all tasks having the specified <code>id</code>.
*
* @param id
* the cancellation identifier
* @param mayInterruptIfRunning
* <code>true</code> if the thread executing this task should be
* interrupted; otherwise, in-progress tasks are allowed to complete
*/
public static synchronized void cancelAll(String id, boolean mayInterruptIfRunning) {
for (int i = TASKS.size() - 1; i >= 0; i--) {
Task task = TASKS.get(i);
if (id.equals(task.id)) {
if (task.future != null) {
task.future.cancel(mayInterruptIfRunning);
if (!task.managed.getAndSet(true)) {
/*
* the task has been submitted to the executor, but its execution has not
* started yet, so that its run() method will never call postExecute()
*/
task.postExecute();
}
} else if (task.executionAsked) {
Log.w(TAG, "A task with id " + task.id + " cannot be cancelled (the executor set does not support it)");
} else {
/* this task has not been submitted to the executor */
TASKS.remove(i);
}
}
}
}
/**
* Checks if the current thread is UI thread and notifies
* {@link BackgroundExecutor.WrongThreadListener#onUiExpected()} if it doesn't.
*/
public static void checkUiThread() {
if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
wrongThreadListener.onUiExpected();
}
}
/**
* Checks if the current thread is a background thread and, optionally,
* restricts it with passed serials. If no serials passed and current thread is
* the UI thread, then {@link WrongThreadListener#onBgExpected(String...)} will
* be called. If the current thread is not UI and serials list is empty, then
* this method just returns. Otherwise, if the method was called not during
* {@link Task} execution or the task has no serial, then the
* {@link WrongThreadListener#onWrongBgSerial(String, String...)} will be called
* with null for the first parameter. If task has a serial but passed serials
* don't contain that, then
* {@link WrongThreadListener#onWrongBgSerial(String, String...)} will be called
* with the task's serial for the first parameter.
*
* @param serials
* (optional) list of allowed serials
*/
public static void checkBgThread(String... serials) {
if (serials.length == 0) {
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
wrongThreadListener.onBgExpected(serials);
}
return;
}
String current = CURRENT_SERIAL.get();
if (current == null) {
wrongThreadListener.onWrongBgSerial(null, serials);
return;
}
for (String serial : serials) {
if (serial.equals(current)) {
return;
}
}
wrongThreadListener.onWrongBgSerial(current, serials);
}
/**
* Indicates whether a task with the specified <code>serial</code> has been
* submitted to the executor.
*
* @param serial
* the serial queue
* @return <code>true</code> if such a task has been submitted,
* <code>false</code> otherwise
*/
private static boolean hasSerialRunning(String serial) {
for (Task task : TASKS) {
if (task.executionAsked && serial.equals(task.serial)) {
return true;
}
}
return false;
}
/**
* Retrieve and remove the first task having the specified <code>serial</code>
* (if any).
*
* @param serial
* the serial queue
* @return task if found, <code>null</code> otherwise
*/
private static Task take(String serial) {
int len = TASKS.size();
for (int i = 0; i < len; i++) {
if (serial.equals(TASKS.get(i).serial)) {
return TASKS.remove(i);
}
}
return null;
}
public static abstract class Task implements Runnable {
private String id;
private long remainingDelay;
private long targetTimeMillis; /* since epoch */
private String serial;
private boolean executionAsked;
private Future<?> future;
/*
* A task can be cancelled after it has been submitted to the executor but
* before its run() method is called. In that case, run() will never be called,
* hence neither will postExecute(): the tasks with the same serial identifier
* (if any) will never be submitted.
*
* Therefore, cancelAll() *must* call postExecute() if run() is not started.
*
* This flag guarantees that either cancelAll() or run() manages this task post
* execution, but not both.
*/
private AtomicBoolean managed = new AtomicBoolean();
public Task(String id, long delay, String serial) {
if (!"".equals(id)) {
this.id = id;
}
if (delay > 0) {
remainingDelay = delay;
targetTimeMillis = SystemClock.elapsedRealtime() + delay;
}
if (!"".equals(serial)) {
this.serial = serial;
}
}
@Override
public void run() {
if (managed.getAndSet(true)) {
/* cancelled and postExecute() already called */
return;
}
try {
CURRENT_SERIAL.set(serial);
execute();
} finally {
/* handle next tasks */
postExecute();
}
}
public abstract void execute();
private void postExecute() {
if (id == null && serial == null) {
/* nothing to do */
return;
}
CURRENT_SERIAL.set(null);
synchronized (BackgroundExecutor.class) {
/* execution complete */
TASKS.remove(this);
if (serial != null) {
Task next = take(serial);
if (next != null) {
if (next.remainingDelay != 0) {
/* the delay may not have elapsed yet */
next.remainingDelay = Math.max(0L, next.targetTimeMillis - SystemClock.elapsedRealtime());
}
/* a task having the same serial was queued, execute it */
BackgroundExecutor.execute(next);
}
}
}
}
}
/**
* A callback interface to be notified when a method invocation is expected from
* another thread.
*
* @see #setWrongThreadListener(WrongThreadListener)
* @see #checkUiThread()
* @see #checkBgThread(String...)
* @see org.androidannotations.annotations.SupposeUiThread
* @see org.androidannotations.annotations.SupposeBackground
*/
public interface WrongThreadListener {
/**
* Will be called, if the method is supposed to be called from the UI-thread,
* but was called from a background thread.
*
* @see org.androidannotations.annotations.SupposeUiThread
* @see #setWrongThreadListener(WrongThreadListener)
* @see #DEFAULT_WRONG_THREAD_LISTENER
*/
void onUiExpected();
/**
* Will be called, if the method is supposed to be called from a background
* thread, but was called from the UI-thread.
*
* @param expectedSerials
* a list of allowed serials. If any background thread is allowed the
* list will be empty.
* @see org.androidannotations.annotations.SupposeBackground
* @see #setWrongThreadListener(WrongThreadListener)
* @see #DEFAULT_WRONG_THREAD_LISTENER
*/
void onBgExpected(String... expectedSerials);
/**
* Will be called, if the method is supposed to be called from a background
* thread with one of {@code expectedSerials}, but was called from a
* {@code currentSerial}. {@code currentSerial} will be null, if it is called
* from a background thread without a serial.
*
* @param currentSerial
* the serial of caller thread or null if there is no serial
* @param expectedSerials
* a list of allowed serials
* @see org.androidannotations.annotations.SupposeBackground
* @see #setWrongThreadListener(WrongThreadListener)
* @see #DEFAULT_WRONG_THREAD_LISTENER
*/
void onWrongBgSerial(String currentSerial, String... expectedSerials);
}
}