public class RunOnUiThreadExecutor
extends java.lang.Object
implements java.util.concurrent.Executor
| Constructor and Description |
|---|
RunOnUiThreadExecutor() |
| Modifier and Type | Method and Description |
|---|---|
void |
execute(java.lang.Runnable command)
Execute given command on the UI thread as soon as possible.
|
void |
executeAsync(java.lang.Runnable command)
Execute the given command asynchronously on the UI thread.
|
public void execute(@NonNull
java.lang.Runnable command)
If the current thread is already the UI one, then the given command is executed directly. Else, it is posted on UI thread for later execution.
execute in interface java.util.concurrent.Executorcommand - to execute.public void executeAsync(@NonNull
java.lang.Runnable command)
In all cases, even if the current thread is the UI one, the given command is posted for later execution.
This may be useful in case of not controlled command to safely execute under locking conditions. For instance:
private Lock _lock = new NonReentrantLock();
public void foo() {
_lock.lock();
new RunOnUiThreadExecutor().executeAsync(this::bar);
_lock.unlock();
}
public void bar() {
_lock.lock();
// ... do stuff
_lock.unlock();
}
command - to execute.