public class BackgroundExecutor
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
BackgroundExecutor.Task |
static interface |
BackgroundExecutor.WrongThreadListener
A callback interface to be notified when a method invocation is expected from another thread.
|
Modifier and Type | Field and Description |
---|---|
static java.util.concurrent.Executor |
DEFAULT_EXECUTOR |
static BackgroundExecutor.WrongThreadListener |
DEFAULT_WRONG_THREAD_LISTENER
The default invocation handler for wrong thread execution.
|
Constructor and Description |
---|
BackgroundExecutor() |
Modifier and Type | Method and Description |
---|---|
static void |
cancelAll(java.lang.String id,
boolean mayInterruptIfRunning)
Cancel all tasks having the specified
id . |
static void |
checkBgThread(java.lang.String... serials)
Checks if the current thread is a background thread and, optionally, restricts it
with passed serials.
|
static void |
checkUiThread()
Checks if the current thread is UI thread and notifies
BackgroundExecutor.WrongThreadListener.onUiExpected() if it doesn't. |
static void |
execute(BackgroundExecutor.Task task)
Execute a task after (at least) its delay and after all
tasks added with the same non-null
serial (if any) have
completed execution. |
static void |
execute(java.lang.Runnable runnable)
Execute a task.
|
static void |
execute(java.lang.Runnable runnable,
int delay)
Execute a task after the given delay.
|
static void |
execute(java.lang.Runnable runnable,
java.lang.String id,
int delay,
java.lang.String serial)
Execute a task.
|
static void |
execute(java.lang.Runnable runnable,
java.lang.String id,
java.lang.String serial)
Execute a task after all tasks added with the same non-null
serial (if any) have completed execution. |
static void |
setExecutor(java.util.concurrent.Executor executor)
Change the executor.
|
static void |
setWrongThreadListener(BackgroundExecutor.WrongThreadListener listener)
Changes the default
BackgroundExecutor.WrongThreadListener . |
public static java.util.concurrent.Executor DEFAULT_EXECUTOR
public static final BackgroundExecutor.WrongThreadListener DEFAULT_WRONG_THREAD_LISTENER
IllegalStateException
with explanation what is going wrong.public static void execute(BackgroundExecutor.Task task)
serial
(if any) have
completed execution.task
- the task to executejava.lang.IllegalArgumentException
- if task.delay
is strictly positive and the
current executor does not support scheduling (if
setExecutor(Executor)
has been called with such an
executor)public static void execute(java.lang.Runnable runnable, java.lang.String id, int delay, java.lang.String serial)
runnable
- the task to executeid
- identifier used for task cancellationdelay
- the time from now to delay execution, in millisecondsserial
- the serial queue (null
or ""
for no
serial execution)java.lang.IllegalArgumentException
- if delay
is strictly positive and the current
executor does not support scheduling (if
setExecutor(Executor)
has been called with such an
executor)public static void execute(java.lang.Runnable runnable, int delay)
runnable
- the task to executedelay
- the time from now to delay execution, in millisecondsjava.lang.IllegalArgumentException
- if delay
is strictly positive and the current
executor does not support scheduling (if
setExecutor(Executor)
has been called with such an
executor)public static void execute(java.lang.Runnable runnable)
runnable
- the task to executepublic static void execute(java.lang.Runnable runnable, java.lang.String id, java.lang.String serial)
serial
(if any) have completed execution.
Equivalent to execute(runnable, id, 0, serial)
.runnable
- the task to executeid
- identifier used for task cancellationserial
- the serial queue to use (null
or ""
for no serial execution)public static void setExecutor(java.util.concurrent.Executor executor)
ScheduledExecutorService
then executing a task after a delay will not be supported anymore. If it
is not even a ExecutorService
then tasks will not be cancellable
anymore.executor
- the new executorpublic static void setWrongThreadListener(BackgroundExecutor.WrongThreadListener listener)
BackgroundExecutor.WrongThreadListener
.
To restore the default one use DEFAULT_WRONG_THREAD_LISTENER
.listener
- the new BackgroundExecutor.WrongThreadListener
public static void cancelAll(java.lang.String id, boolean mayInterruptIfRunning)
id
.id
- the cancellation identifiermayInterruptIfRunning
- true
if the thread executing this task should be
interrupted; otherwise, in-progress tasks are allowed to
completepublic static void checkUiThread()
BackgroundExecutor.WrongThreadListener.onUiExpected()
if it doesn't.public static void checkBgThread(java.lang.String... serials)
BackgroundExecutor.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 BackgroundExecutor.Task
execution or the task has no
serial, then the BackgroundExecutor.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 BackgroundExecutor.WrongThreadListener.onWrongBgSerial(String, String...)
will be called with
the task's serial for the first parameter.serials
- (optional) list of allowed serialsCopyright © 2010-2014. All Rights Reserved.