码迷,mamicode.com
首页 > 编程语言 > 详细

Looper Handler 多线程

时间:2015-05-03 11:59:13      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

Looper is created by default on main UI
    Property:
        // main ui thread, if Looper is initialized in another thread, it would be a new thread
        // class xx extends Thread. => here is a new thread.
        mThread
        mMessageQueue // empty
    loop()
        for(;;) {
            message = mMessageQueue.next()
            message.target.dispatchMessage(message);
        }

Handler
    Property:
        mLooper = Looper.myLooper();
        mQueue = mLooper.mQueue;
        mCallback = callback;
    
    sendMessage
        mQueue.enqueueMessage(msg, uptimeMillis);
    
    interface Callback {
        public boolean handleMessage(Message msg);
    }
    
    dispatchMessage(Message msg) { // ingore Message‘s properties
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        handleMessage(msg);
    }

handler make connection with Looper by adding looper object into handler.    
handler from thread A sends message to looper.MQ
MQ is looped in thread B.
Looper processed MQ messages by sequence from different threads.

Looper Handler 多线程

标签:

原文地址:http://www.cnblogs.com/webglcn/p/4473466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!