2010/03/26 14:38

Looper + (Handler / Message)



Looper.Looper()
Looper객체가 생성될 때 MessageQueue도 같이 생성되며, 생성된 Looper(MessageQueue)객체는 currentThread에 묶인다.
final MessageQueue mQueue;
private Looper() {
        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
}


Looper.loop()
public static final void loop() {
        Looper me = myLooper();//현재 thread의 Looper인스턴스를 불러온다.
        MessageQueue queue = me.mQueue;
        while (true) {
            Message msg = queue.next(); // might block//메세지를 하나 꺼내서
            //if (!me.mRun) {
            //    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);//Message가 가진 Handler를 실행
                if (me.mLogging!= null) me.mLogging.println(
                        "<<<<< Finished to    " + msg.target + " "
                        + msg.callback);
                msg.recycle();//Message는 Recycle pool로.
            }
        }
    }

Reference

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.



간단요약 : MessageHandler를 가지고 있으며, LooperMessageQueue에 있는 Message들을 차례차례 불러와 Handler를 실행시킨다.

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://gloid.egloos.com/tb/37226 [도움말]

덧글

댓글 입력 영역