János János - 2 months ago 7
Android Question

Can Runnable be called immediately?

Is it possible to call

Runnable
without
execute
-ing it?

AsyncTask.execute(new Runnable() {
@Override
public void run() {
//TODO your background code
}
});


In iOS a
closure
can be
executed
, or just called like a method. In first case there is a small delay, other code chunk can come first to the loop, in second case right at the time when call is done code is
Runnable
is performed. How is it with Android?

Answer
Runnable runnable = new Runnable() {...}
runnable.run();

and if you need to be able to decide where to execute it depending on your current thread: UI or non-UI, you can do something like this

if (Looper.getMainLooper().isCurrentThread()) { ... } else { ... }

Some useful information can be found here Runnable