工具类 - 线程切换

线程切换工具类

1. 源码
import android.os.Handler;
import android.os.Looper;

/**
 * 线程切换工具
 */
public class BigUtilsThread {

    private static Handler mHandler = new Handler(Looper.getMainLooper());

    // 在主线程中运行
    public static void runOnUiThread(Runnable r) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            //主线程
            r.run();
        } else {
            //子线程
            mHandler.post(r);
        }
    }

    // 在子线程中运行
    public static void runOnSubThread(Runnable r) {
        new Thread(r).start();
    }
}

发布了25 篇原创文章 · 获赞 2 · 访问量 1491

猜你喜欢

转载自blog.csdn.net/geaosu2/article/details/104407088