[JAVA] Comparison of Android Handler Looper and runOnUiThread

When calling from a thread other than the UI thread, the following two writing methods are the same.

Looper.getMainLooper()



Runnable task = getTask();
new Handler(Looper.getMainLooper()).post(task);

Activity#runOnUiThread()


Runnable task = getTask();
runOnUiThread(task);

When calling from the UI thread, it will be executed directly to the UI thread in the case of runOnUiThread according to the following source.

public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else { //Execute directly for UI thread
        action.run();
    }
}

Recommended Posts

Comparison of Android Handler Looper and runOnUiThread
Thorough comparison of Android and iOS implementations
[Java / Swift] Comparison of Java Interface and Swift Protocol
Comparison of JavaScript objects and Ruby classes
Implementation comparison of production that makes images shine on iOS and Android
Chrome59 Comparison of normal and headless mode operation
The comparison of enums is ==, and equals is good [Java]
Equivalence comparison of Java wrapper classes and primitive types
[Java] String comparison and && and ||
Comparison operators and conditionals
Definition of Android constants
[Java] Comparison method of character strings and comparison method using regular expressions
Comparison of Web App for Containers and Azure Container Instances