本管理器优点:
1.单页面来说,handlerThread作为主要管理方式,一个线程复用,对性能影响小
2.简化代码,一句代码即可开启一个异步任务,一句代码把异步结果传给UI线程
本管理器缺点:
1.由于单线程串行处理任务,有阻塞情况会影响其他任务。
2.每个activity一个管理器(一个线程),当连续开启多个activity的时候,同样对性能影响较大,有OOM风险。
以前写项目的时候,都是直接new Thread()来创建线程,这样写项目后期非常不利于维护,然后考虑是否能写一个类专门用来管理线程呢?
首先我们需要创建一个线程:
private HandlerThread mHandlerThread;
public void oncreate() {
this.mHandlerThread =
new HandlerThread("activity worker:" + this.getClass().getSimpleName());
this.mHandlerThread.start();
}
第二我们线程管理器只是管理线程,不进行具体的逻辑操作,那么就用到了Handler机制:
先创建一个UiHandler:
private static class UiHandler extends Handler {
private HandlerManager manager;
public UiHandler(HandlerManager manager) {
this.manager = manager;
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(manager != null) {
//调用处理业务代码
}
}
}
由于Handler为静态内部类,与外部类并无直接联系,这样处理容易产生内存泄漏,manager对象无法释放,那么就要介入弱引用来处理该代码(也可以用软引用):
private static class UiHandler extends Handler {
private WeakReference mActivityReference;
public UiHandler(HandlerManager manager) {
this.mActivityReference = new WeakReference(manager);
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
//调用处理业务代码
}
}
}
同理还有创建一个异步线程BackgroundHandler,需要注意的是创建该Handler需要把新创建的线程looper传递过去:
private static class BackgroundHandler extends Handler {
private WeakReference mActivityReference;
BackgroundHandler(HandlerManager manager, Looper looper) {
super(looper);
this.mActivityReference = new WeakReference(manager);
}
//发送消息
public void handleMessage(Message msg) {
super.handleMessage(msg);
//调用处理业务代码
}
}
}
这两个Handler我们需要在线程创建的时候就创建,所以OnCreate方法要这样改:
public void oncreate() {
this.mHandlerThread = new HandlerThread("activity worker:" + this.getClass().getSimpleName());
this.mHandlerThread.start();
this.mBackgroundHandler
= new BackgroundHandler(this, this.mHandlerThread.getLooper());
this.mUiHandler = new UiHandler(this);
}
第三我们就要发送消息了,先理清逻辑:
1.通过消息通知Manager有异步请求。
2.Manager给BackgroundHandler发消息让他处理异步逻辑。
3.BackgroundHandler处理异步逻辑,并把处理结果返回给Manager。
4.Manager拿到处理结果发消息给UiHandler。
5.责任人(UiHandler)处理最后逻辑。
1.接收异步请求方法:
public void sendBackgroundMessage(int what, Object object) {
Message message = this.obtainBackgroundMessage();
message.what = what;
message.obj = object;
this.sendBackgroundMessage(message);
}
public Message obtainBackgroundMessage()
return this.mBackgroundHandler.obtainMessage();
}
2.发消息给BackGroundHandler:
public void sendBackgroundMessage(Message message) {
this.mBackgroundHandler.sendMessage(message);
}
3.BackgroudHandler处理异步逻辑:
private static class BackgroundHandler extends Handler {
private WeakReference mActivityReference;
BackgroundHandler(HandlerManager manager, Looper looper) {
super(looper);
this.mActivityReference = new WeakReference(manager);
}
//发送消息
public void handleMessage(Message msg) {
super.handleMessage(msg);
//如果引用不为空,发送消息
if(this.mActivityReference.get() != null) {
((HandlerManager)this.mActivityReference.get()).handleBackgroundMessage(msg);
}
}
}
public abstract void handleBackgroundMessage(Message var1);
4.Manager拿到处理结果发消息给UiHandler:
public void sendUiMessage(int what, Object obj, long delay) {
Message me = this.obtainUiMessage();
me.what = what;
me.obj = obj;
this.sendUiMessage(me, delay);
}
public void sendUiMessage(Message message, long delay) {
this.mUiHandler.sendMessageDelayed(message, delay);
}
5.UiHandler处理最后逻辑:
private static class UiHandler extends Handler {
private WeakReference mActivityReference;
public UiHandler(HandlerManager manager) {
this.mActivityReference = new WeakReference(manager);
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(this.mActivityReference.get() != null) {
((HandlerManager)this.mActivityReference.get()).handleUiMessage(msg);
}
}
}
public abstract void handleUiMessage(Message var1);
最后附带完整代码:
public abstract class HandlerManager {
private HandlerThread mHandlerThread;//含有Looper的Thread
private BackgroundHandler mBackgroundHandler;//后台接收消息的handler载体
private Handler mUiHandler;//主线程接收消息的handler载体
public HandlerManager() {
}
//给主线直接发送message对象
public void sendUiMessage(Message message) {
this.mUiHandler.sendMessage(message);
}
//给主线程发送what和obj,自动组合成message对象
public void sendUiMessage(int what, Object object) {
Message message = this.obtainUiMessage();
message.what = what;
message.obj = object;
this.sendUiMessage(message);
}
//给主线程发送空消息
public void sendUiMessage(int what) {
this.mUiHandler.sendEmptyMessage(what);
}
//给主线程发送延迟消息
public void sendUiMessage(int what, long delay) {
this.mUiHandler.sendEmptyMessageDelayed(what, delay);
}
//给主线程发送what和obj,自动组合成message对象(带延迟)
public void sendUiMessage(int what, Object obj, long delay) {
Message me = this.obtainUiMessage();
me.what = what;
me.obj = obj;
this.sendUiMessage(me, delay);
}
//给主线直接发送message对象(带延迟)
public void sendUiMessage(Message message, long delay) {
this.mUiHandler.sendMessageDelayed(message, delay);
}
//给后台线程发送message对象(带延迟)
public void sendBackgroundMessage(Message message, long delay) {
this.mBackgroundHandler.sendMessageDelayed(message, delay);
}
//给后台线程发送what和obj,自动组合成message对象(带延迟)
public void sendBackgroundMessage(int what, Object object, long delay) {
Message me = this.obtainBackgroundMessage();
me.what = what;
me.obj = object;
this.sendBackgroundMessage(me, delay);
}
public Message obtainUiMessage() {
return this.mUiHandler.obtainMessage();
}
//给后台线程发送message对象
public void sendBackgroundMessage(Message message) {
this.mBackgroundHandler.sendMessage(message);
}
//给后台线程发送what和obj,自动组合成message对象
public void sendBackgroundMessage(int what, Object object) {
Message message = this.obtainBackgroundMessage();
message.what = what;
message.obj = object;
this.sendBackgroundMessage(message);
}
//给后台线程发送空消息
public void sendBackgroundMessage(int what) {
this.mBackgroundHandler.sendEmptyMessage(what);
}
//给主线程发送空消息(带延迟)
public void sendBackgroundMessage(int what, long delay) {
this.mBackgroundHandler.sendEmptyMessageDelayed(what, delay);
}
public Message obtainBackgroundMessage() {
return this.mBackgroundHandler.obtainMessage();
}
//初始化handlermanager对象
public void oncreate() {
this.mHandlerThread
= new HandlerThread("activity worker:"
+ this.getClass().getSimpleName());
this.mHandlerThread.start();
this.mBackgroundHandler
= new BackgroundHandler(this, this.mHandlerThread.getLooper());
this.mUiHandler = new UiHandler(this);
}
//清空HandlerManager对象中数据,防止内存泄漏
public void onDestroy() {
if(this.mBackgroundHandler != null &&
this.mBackgroundHandler.getLooper() != null) {
this.mBackgroundHandler.getLooper().quit();
}
}
public abstract void handleBackgroundMessage(Message var1);
public abstract void handleUiMessage(Message var1);
/**
* UI线程
* */
private static class UiHandler extends Handler {
//HandlerManager 的弱引用,防止内存泄漏
private WeakReference mActivityReference;
public UiHandler(HandlerManager manager) {
this.mActivityReference = new WeakReference(manager);
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
//如果引用不为空,发送消息
if(this.mActivityReference.get() != null) {
((HandlerManager)this.mActivityReference.get())
.handleUiMessage(msg);
}
}
}
/**
* 后台线程
* */
private static class BackgroundHandler extends Handler {
//HandlerManager 的弱引用,防止内存泄漏
private WeakReference mActivityReference;
BackgroundHandler(HandlerManager manager, Looper looper) {
super(looper);
this.mActivityReference = new WeakReference(manager);
}
//发送消息
public void handleMessage(Message msg) {
super.handleMessage(msg);
//如果引用不为空,发送消息
if(this.mActivityReference.get() != null) {
((HandlerManager)this.mActivityReference.get())
.handleBackgroundMessage(msg);
}
}
}
下面是调用代码(建议写在BaseActivity中):
private HandlerManager handlerManager;//线程管理器
private void initHandle() {
this.handlerManager = new HandlerManager() {
public void handleBackgroundMessage(Message message) {
BaseActivity.this.handleBackgroundMessage(message);
}
public void handleUiMessage(Message message) {
BaseActivity.this.handleUiMessage(message);
}
};
this.handlerManager.oncreate();
}
public void onDestroy() {
super.onDestroy();
this.handlerManager.onDestroy();
}
@Override
public void sendUiMessage(Message var1) {
handlerManager.sendUiMessage(var1);
}
@Override
public void sendUiMessage(int var1, Object var2) {
handlerManager.sendUiMessage(var1, var2);
}
@Override
public void sendUiMessage(int var1) {
handlerManager.sendUiMessage(var1);
}
@Override
public void sendUiMessage(int var1, long var2) {
handlerManager.sendUiMessage(var1, var2);
}
@Override
public void sendUiMessage(int var1, Object var2, long var3) {
handlerManager.sendUiMessage(var1, var2, var3);
}
@Override
public void sendUiMessage(Message var1, long var2) {
handlerManager.sendUiMessage(var1, var2);
}
@Override
public void sendBackgroundMessage(Message var1) {
handlerManager.sendBackgroundMessage(var1);
}
@Override
public void sendBackgroundMessage(int var1, Object var2) {
handlerManager.sendBackgroundMessage(var1,var2);
}
@Override
public void sendBackgroundMessage(int var1) {
handlerManager.sendBackgroundMessage(var1);
}
@Override
public void sendBackgroundMessage(int var1, long var2) {
handlerManager.sendBackgroundMessage(var1,var2);
}
@Override
public void sendBackgroundMessage(int var1, Object var2, long var3) {
handlerManager.sendBackgroundMessage(var1,var2,var3);
}
@Override
public void sendBackgroundMessage(Message var1, long var2) {
handlerManager.sendBackgroundMessage(var1,var2);
}
@Override
public void handleBackgroundMessage(Message var1) {
}
@Override
public void handleUiMessage(Message var1) {
}
推荐下本人的微信公众号,本博客及其他方面的消息会定期同步到公众号上面!