日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Android項(xiàng)目開(kāi)發(fā)常用工具類(lèi)LightTaskUtils源碼介紹_Android

作者:谷哥的小弟 ? 更新時(shí)間: 2022-07-31 編程語(yǔ)言
  • 版權(quán)聲明 本文原創(chuàng)作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

LightTaskUtils概述

LightTaskUtils是一個(gè)輕量級(jí)的線(xiàn)程管理工具。

LightTaskUtils截圖

LightTaskUtils截圖如下:

LightTaskUtils源碼

LightTaskUtils源碼如下:

import android.os.HandlerThread;
import android.os.Handler;

/**
 * 輕量級(jí)的線(xiàn)程管理工具類(lèi)
 *
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */

public class LightTaskUtils {

    private Handler mHandler;
    private HandlerThread mHandlerThread;

    public LightTaskUtils() {
        mHandlerThread = new HandlerThread("LightTaskThread");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
    }

    /**
     * 普通操作,線(xiàn)程優(yōu)先級(jí)比UI線(xiàn)程底,用于無(wú)UI交互操作
     */
    public void post(Runnable runnable) {
        if (mHandler != null) {
            mHandler.post(runnable);
        }
    }

    /**
     * 線(xiàn)程優(yōu)先級(jí)和UI線(xiàn)程一樣,用于UI交互操作
     */
    public void postAtFrontOfQueue(Runnable runnable) {
        if (mHandler != null) {
            mHandler.postAtFrontOfQueue(runnable);
        }
    }

    public void postDelayed(Runnable runnable, long delay) {
        if (mHandler != null) {
            mHandler.postDelayed(runnable, delay);
        }
    }

    public void postAtTime(Runnable runnable, long time) {
        if (mHandler != null) {
            mHandler.postAtTime(runnable, time);
        }
    }

    /**
     * 清空Handler消息棧和子線(xiàn)程的Loop,避免內(nèi)存泄漏
     */
    public void removePost() {
        if (mHandlerThread != null) {
            mHandlerThread.quit();
            mHandlerThread = null;
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
    }
}

原文鏈接:https://blog.csdn.net/lfdfhl/article/details/125100692

欄目分類(lèi)
最近更新