android異步消息處理主要由四部分組成:Handler,Looper,Message,MessageQueuejava
Message:線程之間傳遞的消息,能夠在內部攜帶少許消息android
MessageQueue:異步
Looper:每一個線程有且最多隻能有一個Looper對象,它是一個ThreadLocal(線程本地存儲對象);oop
(ThreadLocal:實現一個線程本地存儲對象,對於一個變量,每一個線程都要各自的值,全部線程共享相同的對象,可是其中一個線程中的變量改變不會影響其餘線程中該變量的值)線程
/** * Implements a thread-local storage, that is, a variable for which each thread * has its own value. All threads share the same {@code ThreadLocal} object, * but each sees a different value when accessing it, and changes made by one * thread do not affect the other threads. The implementation supports * {@code null} values. * * @see java.lang.Thread * @author Bob Lee */
Looper內部維護了一個MQ,loop()方法調用後線程開始不斷從隊列中取出消息執行code
Handler:1.用於處理和發送消息;對象
2.建立時關聯一個Looper和Looper中的MQ;隊列
3.一個線程能夠有多個Handler;it
4.Handler可在任意線程發送消息,消息會被添加到關聯的MQ中;io
5.Handler是在它關聯的Looper線程中處理消息的。