设为首页 加入收藏

TOP

Android开发实践:自定义带消息循环(Looper)的工作线程
2015-02-02 14:37:50 来源: 作者: 【 】 浏览:9
Tags:Android 开发 实践 定义 消息 循环 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;? ? ?


}


到这里,一个基本的带有消息循环的线程类封装就完成了,相信大家应该从编写这段代码的过程中,理解了系统是如何实现消息循环的。完整的代码见博文最后的附件,有任何疑问欢迎留言或者来信lujun.hust@gmail.com交流。


相关附件下载地址


------------------------------------------分割线------------------------------------------


具体下载目录在 /2014年资料/12月/4日/Android开发实践:自定义带消息循环(Looper)的工作线程


------------------------------------------分割线------------------------------------------


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发实践:由new Handler(.. 下一篇Android开发实践:为什么要继承on..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: