android 自定义looper,Android 线程之自定义带消息循环Looper的实例

Android 线程之自定义带消息循环Looper的实例

Android系统的UI线程是一种带消息循环(Looper)机制的线程,同时Android也提供了封装有消息循环(Looper)的HandlerThread类,这种线程,可以绑定Handler()对象,并通过Handler的sendMessage()函数向线程发送消息,通过handleMessage()函数,处理线程接收到的消息。这么说比较抽象,那么,本文就利用基础的Java类库,实现一个带消息循环(Looper)的线程,以帮助初学者理解这样一个Looper到底是怎么工作的。

1. 首先,我们完成一个简单的线程框架。

public class LooperThread {

private volatile boolean mIsLooperQuit = false;

private Thread mThread;

public void start() {

if( mThread != null ) {

return;

}

mIsLooperQuit = false;

mThread = new Thread(mLooperRunnable);

mThread.start();

}

public void stop() {

if( mThread == null ) {

return;

}

mIsLooperQuit = true;

mThread = null;

}

protected Runnable mLooperRunnable = new Runnable() {

@Override

public void run() {

while( !mIsLooperQuit ) {

}

}

};

}

如上述代码所示,mLooperRunnable.run()循环执行线程任务,mIsLooperQuit则是线程退出循环的条件。下面,我们将添加消息的发送和处理代码。

2. 添加线程循环的消息发送和处理代码

(1) 定义消息结构体,创建消息队列

public class LooperThread {

private Queue mMessageQueue = new LinkedList();

public static class Message {

int what;

}

}

(2) 创建互斥锁和条件变量

public class LooperThread {

private Lock mLock = new ReentrantLock();

private Condition mCondition = mLock.newCondition();

}

(3) 创建发送消息的函数

//发送消息,由外部其他模块调用,发送消息给线程

public void sendMessage( Message message ) {

if( mThread == null ) {

return;

}

mLock.lock();

mMessageQueue.add(message); //添加消息到消息队列

mCondition.signal(); //通知线程循环,有消息来了,请立即处理

mLock.unlock();

}

(4) 创建处理消息的函数

//处理消息,由线程内部调用

public void handleMessage(Message message) {

//这里可以通过一个Callback来回调监听者

}

(5) 在mLooperRunnable.run()循环中解析消息

protected Runnable mLooperRunnable = new Runnable() {

@Override

public void run() {

while( !mIsLooperQuit ) {

mLock.lock();

Message message = null;

try {

while( !mIsLooperQuit && mMessageQueue.isEmpty() ) {

mCondition.await(); //没有消息到来则休眠

}

message = mMessageQueue.poll();

}

catch (InterruptedException e) {

e.printStackTrace();

}

finally {

mLock.unlock();

}

handleMessage(message );

}

};

}

(6) 修改线程的Stop()函数,唤醒休眠的消息循环

public void stop() {

if( mThread == null ) {

return;

}

mIsLooperQuit = true;

mLock.lock();

mCondition.signal();

mLock.unlock();

mMessageQueue.clear();

mThread = null;

}

到这里,一个基本的带有消息循环的线程类封装就完成了,相信大家应该从编写这段代码的过程中,理解了系统是如何实现消息循环的。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值