目录
- 版权声明 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
LightTaskUtils概述
LightTaskUtils是一个轻量级的线程管理工具。
LightTaskUtils截图
LightTaskUtils截图如下:
LightTaskUtils源码
LightTaskUtils源码如下:
import android.os.HandlerThread; import android.os.Handler; /** * 轻量级的线程管理工具类 * * 本文作者:谷哥的小弟 * 博客地址: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()); } /** * 普通操作,线程优先级比UI线程底,用于无UI交互操作 */ public void post(Runnable runnable) { if (mHandler != null) { mHandler.post(runnable); } } /** * 线程优先级和UI线程一样,用于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消息栈和子线程的Loop,避免内存泄漏 */ public void removePost() { if (mHandlerThread != null) { mHandlerThread.quit(); mHandlerThread = null; } if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); mHandler = null; } } }