您的位置:首页 > 移动开发 > Android开发

深入理解Android消息处理系统——Looper、Handler、Thread

2011-11-21 09:21 465 查看
熟悉Windows编程的朋友可能知道Windows程序是消息驱动的,并且有全局的消息循环系统。而Android应用程序也是消息驱动的,按道理来说也应该提供消息循环机制。实际上谷歌参考了Windows的消息循环机制,也在Android系统中实现了消息循环机制。Android通过Looper、Handler来实现消息循环机制,Android消息循环是针对线程的(每个线程都可以有自己的消息队列和消息循环)。本文深入介绍一下Android消息处理系统原理。

Android系统中Looper负责管理线程的消息队列和消息循环,具体实现请参考Looper的源码。 可以通过Loop.myLooper()得到当前线程的Looper对象,通过Loop.getMainLooper()可以获得当前进程的主线程的Looper对象。

前面提到Android系统的消息队列和消息循环都是针对具体线程的,一个线程可以存在(当然也可以不存在)一个消息队列和一个消息循环(Looper),特定线程的消息只能分发给本线程,不能进行跨线程,跨进程通讯。但是创建的工作线程默认是没有消息循环和消息队列的,如果想让该线程具有消息队列和消息循环,需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。如下例所示:

class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }


这样你的线程就具有了消息处理机制了,在Handler中进行消息处理。

Activity是一个UI线程,运行于主线程中,Android系统在启动的时候会为Activity创建一个消息队列和消息循环(Looper)。详细实现请参考ActivityThread.java文件。

Handler的作用是把消息加入特定的(Looper)消息队列中,并分发和处理该消息队列中的消息。构造Handler的时候可以指定一个Looper对象,如果不指定则利用当前线程的Looper创建。详细实现请参考Looper的源码。

Activity、Looper、Handler的关系如下图所示:



一个Activity中可以创建多个工作线程或者其他的组件,如果这些线程或者组件把他们的消息放入Activity的主线程消息队列,那么该消息就会在主线程中处理了。因为主线程一般负责界面的更新操作,并且Android系统中的weget不是线程安全的,所以这种方式可以很好的实现Android界面更新。在Android系统中这种方式有着广泛的运用。

那么另外一个线程怎样把消息放入主线程的消息队列呢?答案是通过Handle对象,只要Handler对象以主线程的Looper创建,那么调用Handler的sendMessage等接口,将会把消息放入队列都将是放入主线程的消息队列。并且将会在Handler主线程中调用该handler的handleMessage接口来处理消息。

这里面涉及到线程同步问题,请先参考如下例子来理解Handler对象的线程模型:

1、首先创建MyHandler工程。

2、在MyHandler.java中加入如下的代码:

package com.simon;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.os.Handler;

public class MyHandler extends Activity {
	static final String TAG = "Handler";
	Handler h = new Handler(){
    	public void handleMessage (Message msg)
    	{
    		switch(msg.what)
    		{
    		case HANDLER_TEST:
    			Log.d(TAG, "The handler thread id = " + Thread.currentThread().getId() + "\n");
    			break;
    		}
    	}
    };

	static final int HANDLER_TEST = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "The main thread id = " + Thread.currentThread().getId() + "\n");

        new myThread().start();
        setContentView(R.layout.main);
    }

    class myThread extends Thread
    {
    	public void run()
    	{
    		Message msg = new Message();
    		msg.what = HANDLER_TEST;
    		h.sendMessage(msg);
    		Log.d(TAG, "The worker thread id = " + Thread.currentThread().getId() + "\n");
    	}
    }
}


在这个例子中我们主要是打印,这种处理机制各个模块的所处的线程情况。如下是我的机器运行结果:
09-10 23:40:51.478: DEBUG/Handler(302): The main thread id = 1
09-10 23:40:51.569: DEBUG/Handler(302): The worker thread id = 8
09-10 23:40:52.128: DEBUG/Handler(302): The handler thread id = 1


我们可以看出消息处理是在主线程中处理的,在消息处理函数中可以安全的调用主线程中的任何资源,包括刷新界面。工作线程和主线程运行在不同的线程中,所以必须要注意这两个线程间的竞争关系。

上例中,你可能注意到在工作线程中访问了主线程handler对象,并在调用handler的对象向消息队列加入了一个消息。这个过程中会不会出现消息队列数据不一致问题呢?答案是handler对象不会出问题,因为handler对象管理的Looper对象是线程安全的,不管是加入消息到消息队列和从队列读出消息都是有同步对象保护的,具体请参考Looper.java文件。上例中没有修改handler对象,所以handler对象不可能会出现数据不一致的问题。

通过上面的分析,我们可以得出如下结论:

1、如果通过工作线程刷新界面,推荐使用handler对象来实现。

2、注意工作线程和主线程之间的竞争关系。推荐handler对象在主线程中构造完成(并且启动工作线程之后不要再修改之,否则会出现数据不一致),然后在工作线程中可以放心的调用发送消息SendMessage等接口。

3、除了2所述的hanlder对象之外的任何主线程的成员变量如果在工作线程中调用,仔细考虑线程同步问题。如果有必要需要加入同步对象保护该变量。

4、handler对象的handleMessage接口将会在主线程中调用。在这个函数可以放心的调用主线程中任何变量和函数,进而完成更新UI的任务。

5、Android很多API也利用Handler这种线程特性,作为一种回调函数的变种,来通知调用者。这样Android框架就可以在其线程中将消息发送到调用者的线程消息队列之中,不用担心线程同步的问题。

深入理解Android消息处理机制对于应用程序开发非常重要,也可以让你对线程同步有更加深刻的认识。以上是最近Simon学习Android消息处理机制的一点儿总结,如有错误之处请不吝指教。

参考资料:

http://www.wscxy.com/nuaa/article.asp?id=116

http://www.android1.net/Topic.aspx?BoardID=11&TopicID=631&Page=1

http://www.android1.net/Topic.aspx?BoardID=11&TopicID=632

http://www.android1.net/Topic.aspx?BoardID=11&TopicID=625

Android系统集成第三方pre-build库和程序
Android系统的Binder机制之二——服务代理对象(1)
Android系统的Binder机制之一——Service Manager
(转)高焕堂——Android框架底层结构知多少?
Android JNI编程提高篇之二
Android JNI编程提高篇之一
Android JNI开发入门之二
Android JNI开发入门之一
Android应用开发之Java基础篇——内部类
观察者(Observer)模式在Android应用
模板方法(Template method)模式在Android应用
Android利用VideoView实现VideoPlayer
高焕堂Android培训讲义(转)
Android应用程序的粘合剂——intent
图像解码之三——giflib解码gif图片

转贴:http://my.unix-center.net/~Simon_fu/?p=652

============================================================================================================

Android Looper和Handler分析

第一次接触android应用程序(这里指的是J***A层的UI程序,也难怪了,Google放出的API就只支持J***A应用程序了),很难搞明白内部是如何实现的。但是,从原理上分析,应该是有一个消息循环,一个消息队列,然后主线程不断得从消息队列中取得消息并处理之。

然而,google封装得太厉害了,所以一时半会还是搞不清楚到底是怎么做的。本文将分析android内的looper,这个是用来封装消息循环和消息队列的一个类,handler其实可以看做是一个工具类,用来向消息队列中插入消息的。好比是Windows API的SendMessage中的HANDLE,这个handle是窗口句柄。

view plain

//Looper类分析
//没找到合适的分析代码的办法,只能这么来了。每个重要行的上面都会加上注释
//功能方面的代码会在代码前加上一段分析
public class Looper {
//static变量,判断是否打印调试信息。
private static final boolean DEBUG = false;
private static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;

// sThreadLocal.get() will return null unless you've called prepare().
//线程本地存储功能的封装,TLS,thread local storage,什么意思呢?因为存储要么在栈上,例如函数内定义的内部变量。要么在堆上,例如new或者malloc出来的东西
//但是现在的系统比如Linux和windows都提供了线程本地存储空间,也就是这个存储空间是和线程相关的,一个线程内有一个内部存储空间,这样的话我把线程相关的东西就存储到
//这个线程的TLS中,就不用放在堆上而进行同步操作了。
private static final ThreadLocal sThreadLocal = new ThreadLocal();
//消息队列,MessageQueue,看名字就知道是个queue..
final MessageQueue mQueue;
volatile boolean mRun;
//和本looper相关的那个线程,初始化为null
Thread mThread;
private Printer mLogging = null;
//static变量,代表一个UI Process(也可能是service吧,这里默认就是UI)的主线程
private static Looper mMainLooper = null;

/** Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
* {@link #loop()} after calling this method, and end it by calling
* {@link #quit()}.
*/
//往TLS中设上这个Looper对象的,如果这个线程已经设过了looper的话就会报错
//这说明,一个线程只能设一个looper
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper());
}

/** Initialize the current thread as a looper, marking it as an application's main
* looper. The main looper for your application is created by the Android environment,
* so you should never need to call this function yourself.
* {@link #prepare()}
*/
//由framework设置的UI程序的主消息循环,注意,这个主消息循环是不会主动退出的
//
public static final void prepareMainLooper() {
prepare();
setMainLooper(myLooper());
//判断主消息循环是否能退出....
//通过quit函数向looper发出退出申请
if (Process.supportsProcesses()) {
myLooper().mQueue.mQuitAllowed = false;
}
}

private synchronized static void setMainLooper(Looper looper) {
mMainLooper = looper;
}

/** Returns the application's main looper, which lives in the main thread of the application.
*/
public synchronized static final Looper getMainLooper() {
return mMainLooper;
}

/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
//消息循环,整个程序就在这里while了。
//这个是static函数喔!
public static final void loop() {
Looper me = myLooper();//从该线程中取出对应的looper对象
MessageQueue queue = me.mQueue;//取消息队列对象...
while (true) {
Message msg = queue.next(); // might block取消息队列中的一个待处理消息..
//if (!me.mRun) {//是否需要退出?mRun是个volatile变量,跨线程同步的,应该是有地方设置它。
// break;
//}
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message.
return;
}
if (me.mLogging!= null) me.mLogging.println(
">>>>> Dispatching to " + msg.target + " "
+ msg.callback + ": " + msg.what
);
msg.target.dispatchMessage(msg);
if (me.mLogging!= null) me.mLogging.println(
"<<<<< Finished to " + msg.target + " "
+ msg.callback);
msg.recycle();
}
}
}

/**
* Return the Looper object associated with the current thread. Returns
* null if the calling thread is not associated with a Looper.
*/
//返回和线程相关的looper
public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}

/**
* Control logging of messages as they are processed by this Looper. If
* enabled, a log message will be written to <var>printer</var>
* at the beginning and ending of each message dispatch, identifying the
* target Handler and message contents.
*
* @param printer A Printer object that will receive log messages, or
* null to disable message logging.
*/
//设置调试输出对象,looper循环的时候会打印相关信息,用来调试用最好了。
public void setMessageLogging(Printer printer) {
mLogging = printer;
}

/**
* Return the {@link MessageQueue} object associated with the current
* thread. This must be called from a thread running a Looper, or a
* NullPointerException will be thrown.
*/
public static final MessageQueue myQueue() {
return myLooper().mQueue;
}
//创建一个新的looper对象,
//内部分配一个消息队列,设置mRun为true
private Looper() {
mQueue = new MessageQueue();
mRun = true;
mThread = Thread.currentThread();
}

public void quit() {
Message msg = Message.obtain();
// NOTE: By enqueueing directly into the message queue, the
// message is left with a null target. This is how we know it is
// a quit message.
mQueue.enqueueMessage(msg, 0);
}

/**
* Return the Thread associated with this Looper.
*/
public Thread getThread() {
return mThread;
}
//后面就简单了,打印,异常定义等。
public void dump(Printer pw, String prefix) {
pw.println(prefix + this);
pw.println(prefix + "mRun=" + mRun);
pw.println(prefix + "mThread=" + mThread);
pw.println(prefix + "mQueue=" + ((mQueue != null) ? mQueue : "(null"));
if (mQueue != null) {
synchronized (mQueue) {
Message msg = mQueue.mMessages;
int n = 0;
while (msg != null) {
pw.println(prefix + " Message " + n + ": " + msg);
n++;
msg = msg.next;
}
pw.println(prefix + "(Total messages: " + n + ")");
}
}
}

public String toString() {
return "Looper{"
+ Integer.toHexString(System.identityHashCode(this))
+ "}";
}

static class HandlerException extends Exception {

HandlerException(Message message, Throwable cause) {
super(createMessage(cause), cause);
}

static String createMessage(Throwable cause) {
String causeMsg = cause.getMessage();
if (causeMsg == null) {
causeMsg = cause.toString();
}
return causeMsg;
}
}
}

那怎么往这个消息队列中发送消息呢??调用looper的static函数myQueue可以获得消息队列,这样你就可用自己往里边插入消息了。不过这种方法比较麻烦,这个时候handler类就发挥作用了。先来看看handler的代码,就明白了。

view plain

class Handler{
..........
//handler默认构造函数
public Handler() {
//这个if是干嘛用的暂时还不明白,涉及到java的深层次的内容了应该
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
//获取本线程的looper对象
//如果本线程还没有设置looper,这回抛异常
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
//无耻啊,直接把looper的queue和自己的queue搞成一个了
//这样的话,我通过handler的封装机制加消息的话,就相当于直接加到了looper的消息队列中去了
mQueue = mLooper.mQueue;
mCallback = null;
}
//还有好几种构造函数,一个是带callback的,一个是带looper的
//由外部设置looper
public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
}
// 带callback的,一个handler可以设置一个callback。如果有callback的话,
//凡是发到通过这个handler发送的消息,都有callback处理,相当于一个总的集中处理
//待会看dispatchMessage的时候再分析
public Handler(Looper looper, Callback callback) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
}
//
//通过handler发送消息
//调用了内部的一个sendMessageDelayed
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}
//FT,又封装了一层,这回是调用sendMessageAtTime了
//因为延时时间是基于当前调用时间的,所以需要获得绝对时间传递给sendMessageAtTime
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}


public boolean sendMessageAtTime(Message msg, long uptimeMillis)
{
boolean sent = false;
MessageQueue queue = mQueue;
if (queue != null) {
//把消息的target设置为自己,然后加入到消息队列中
//对于队列这种数据结构来说,操作比较简单了
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
}
else {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
}
return sent;
}
//还记得looper中的那个消息循环处理吗
//从消息队列中得到一个消息后,会调用它的target的dispatchMesage函数
//message的target已经设置为handler了,所以
//最后会转到handler的msg处理上来
//这里有个处理流程的问题
public void dispatchMessage(Message msg) {
//如果msg本身设置了callback,则直接交给这个callback处理了
if (msg.callback != null) {
handleCallback(msg);
} else {
//如果该handler的callback有的话,则交给这个callback处理了---相当于集中处理
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
//否则交给派生处理,基类默认处理是什么都不干
handleMessage(msg);
}
}
..........
}

讲了这么多,该怎么创建和使用一个带消息循环的线程呢?

view plain

//假设在onCreate中创建一个线程
//不花时间考虑代码的完整和严谨性了,以讲述原理为主。
....

... onCreate(...){

//难点是如何把android中的looper和java的thread弄到一起去。
//而且还要把随时取得这个looper用来创建handler
//最简单的办法就是从Thread派生一个
class ThreadWithMessageHandle extends Thread{
//重载run函数
Looper myLooper = null;
run(){
Looper.prepare();//将Looper设置到这个线程中
myLooper = Looper.myLooper();
Looper.loop();开启消息循环
}

ThreadWithMessageHandle threadWithMgs = new ThreadWithMessageHandle();
threadWithMsg.start();
Looper looper = threadWithMsg.myLooper;//
//这里有个问题.threadWithMgs中的myLooper可能此时为空
//需要同步处理一下
//或者像API文档中的那样,把handler定义到ThreadWithMessageHandle到去。
//外线程获得这个handler的时候仍然要注意同步的问题,因为handler的创建是在run中的
Handler threadHandler = new Handler(looper);
threadHandler.sendMessage(...)
}


}



...

好了,handler和looper的分析就都这了,其实原理挺简单的。

转贴:http://blog.csdn.net/Innost/article/details/6055793
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: