线程工具类

ThreadUtil.postDelayedOnWorkThread(() -> {
// 处理逻辑
}, LengthTime);
public class ThreadUtil {
    private static final String TAG = ThreadUtil.class.getSimpleName();

    private static class HandlerHolder {
        public final static Handler sInstance = new Handler(Looper.getMainLooper());
    }

    /**
     * Posts a runnable to the UI thread.
     */
    public static void postOnUiThread(Runnable runnable) {
        getUiThreadHandler().post(runnable);
    }

    /**
     * Posts a runnable to the UI thread, to be run after the specified amount of time elapses.
     */
    public static void postDelayedOnUiThread(Runnable runnable, long delayMillis) {
        getUiThreadHandler().postDelayed(runnable, delayMillis);
    }

    /**
     * Gets a handler which uses the main looper.
     */
    public static Handler getUiThreadHandler() {
        return HandlerHolder.sInstance;
    }

    private static class WorkHandlerHolder {

        public static Handler INSTANCE = getHandler();

        private static Handler getHandler() {
            HandlerThread thread = new HandlerThread("Work-thread");
            thread.start();
            return new Handler(thread.getLooper());
        }
    }

    /**
     * Posts a runnable to the work thread.
     * <p>
     * 这是一个单线程池,不适合做长耗时的操作
     * <p>
     * 文件读写,网络请求等长耗时的工作 请使用{@link SimpleTask} or {@link ThreadPoolManager}.
     */
    public static void postOnWorkThread(Runnable runnable) {
        getWorkHandler().post(runnable);
    }

    /**
     * Posts a runnable to the work thread, to be run after the specified amount of time elapses.
     * <p>
     * 这是一个单线程池,不适合做长耗时的操作
     * <p>
     * 文件读写,网络请求等长耗时的工作 请使用{@link SimpleTask} or {@link ThreadPoolManager}.
     */
    public static void postDelayedOnWorkThread(Runnable runnable, long delayMillis) {
        getWorkHandler().postDelayed(runnable, delayMillis);
    }

    /**
     * Remove any pending posts of Runnable r that are in the message queue.
     */
    public static void removeCallbacksOnWorkThread(Runnable runnable) {
        getWorkHandler().removeCallbacks(runnable);
    }

    /**
     * Gets a handler which uses the work thread looper.
     */
    public static Handler getWorkHandler() {
        return WorkHandlerHolder.INSTANCE;
    }

    public static void checkAndRunOnUiThread(Runnable r) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            r.run();
        } else {
            postOnUiThread(r);
        }
    }

    public static abstract class TraceTask implements Runnable {
        private String mTraceName;

        public TraceTask(String traceName) {
            mTraceName = traceName;
        }

        /**
         * 需要重写的方法
         */
        public abstract void runTask();

        @Override
        public void run() {
            Trace.beginSection(mTraceName);
            runTask();
            Trace.endSection();
        }
    }

    public static void slientSleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (Exception e) {
            Log.w(TAG, "slient sleep " + e.toString());
        }
    }

    private static Executor workExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread result = new Thread(r);
            result.setName("EXECUTOR-WORK");
            return result;
        }
    });
    private static Scheduler workScheduler = Schedulers.from(workExecutor);


    public static Executor getWorkExecutor() {
        return workExecutor;
    }

    public static Scheduler getWorkScheduler() {
        return workScheduler;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值