工具类

1. 运行在主线程

public class ThreadUtils {/**
     * Run a runnable on the Main (UI) Thread.
     * @param runnable the runnable
     */
    public static void runOnUiThread(final Runnable runnable) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            new Handler(Looper.getMainLooper()).post(runnable);
        } else {
            runnable.run();
        }
    }

/**
* Returns {@code true} if called on the main thread, {@code false} otherwise.
*/
public static boolean isOnMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}

}

参考: Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作

原文地址:https://www.cnblogs.com/huyang011/p/7666374.html