voidcheckThread(){if(mThread !=Thread.currentThread()){thrownewCalledFromWrongThreadException("Only the original thread that created a view hierarchy can touch its views.");}}
// 使用ThreadLocacl保存Looper对象,ThreadLocal为线程变量,其填充的变量只属于当前线程,故一个线程有唯一的一个LooperstaticfinalThreadLocal<Looper> sThreadLocal =newThreadLocal<Looper>();// 获取当前线程的Looperpublicstatic@NullableLoopermyLooper(){return sThreadLocal.get();}// Looper对象的创建publicstaticvoidprepare(){prepare(true);}privatestaticvoidprepare(boolean quitAllowed){if(sThreadLocal.get()!=null){thrownewRuntimeException("Only one Looper may be created per thread");}
sThreadLocal.set(newLooper(quitAllowed));}// MessageQueue对象的创建,其在Looper构造器中,因为Looper唯一,所以MessageQueue也是唯一的privateLooper(boolean quitAllowed){
mQueue =newMessageQueue(quitAllowed);
mThread =Thread.currentThread();}//Handler构造器为public,故其可以创建多个
//MessageQueue中的enqueueMessage方法是插入消息的方法,该方法通过synchronized进行同步,保证线程安全,其中this就是当前的消息队列booleanenqueueMessage(Message msg,long when){if(msg.target ==null){thrownewIllegalArgumentException("Message must have a target.");}synchronized(this){....}returntrue;}
子线程中维护的Looper,消息队列无消息的时候的处理方案是什么?有什么用?
//Looper.loop()publicstaticvoidloop(){finalLooper me =myLooper();...for(;;){// 调用loopOnce取出消息,该接口返回值表示当前循环是否继续if(!loopOnce(me, ident, thresholdOverride)){return;}}}//Looper.loopOnce()privatestaticbooleanloopOnce(finalLooper me,finallong ident,finalint thresholdOverride){// 取出下一个消息,如果没有消息,返回当前外循环不在继续,则loop的循环结束 Message msg = me.mQueue.next();// might blockif(msg ==null){// No message indicates that the message queue is quitting.returnfalse;}...
msg.recycleUnchecked();returntrue;}//MessageQueue.next():从消息队列中取出下一个消息Messagenext(){// Return here if the message loop has already quit and been disposed.// This can happen if the application tries to restart a looper after quit// which is not supported.finallong ptr = mPtr;if(ptr ==0){returnnull;}int pendingIdleHandlerCount =-1;// -1 only during first iterationint nextPollTimeoutMillis =0;for(;;){if(nextPollTimeoutMillis !=0){Binder.flushPendingCommands();}// 该方法为阻塞方法,其中nextPollTimeoutMillis为阻塞时间nativePollOnce(ptr, nextPollTimeoutMillis);// 存在消息synchronized(this){...}....}}
//android_os_MessageQueue.cpp中的方法,其为静态方法,会调用内部的pollOnce方法staticvoidandroid_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj,
jlong ptr, jint timeoutMillis){NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->pollOnce(env, obj, timeoutMillis);}//android_os_MessageQueue.cpp中的方法,最后调用Looper.cpp中的pollOnce方法voidNativeMessageQueue::pollOnce(JNIEnv* env, jobject pollObj,int timeoutMillis){
mPollEnv = env;
mPollObj = pollObj;
mLooper->pollOnce(timeoutMillis);
mPollObj = NULL;
mPollEnv = NULL;if(mExceptionObj){
env->Throw(mExceptionObj);
env->DeleteLocalRef(mExceptionObj);
mExceptionObj = NULL;}}// Looper.cpp中的pollOnce方法中存在一个死循环,用于处理事件intLooper::pollOnce(int timeoutMillis,int* outFd,int* outEvents,void** outData){int result =0;for(;;){while(mResponseIndex < mResponses.size()){constResponse& response = mResponses.itemAt(mResponseIndex++);int ident = response.request.ident;// 当存在消息时,返回消息标识if(ident >=0){...return ident;}}// 通过pollInner获取当前的消息表示,存在消息则返回,否则继续循环遍历if(result !=0){...return result;}// 取出
result =pollInner(timeoutMillis);}}//Looper.cpp中的pollInner()为最终决定是否阻塞的条件intLooper::pollInner(int timeoutMillis){// Poll.int result = POLL_WAKE;
struct epoll_event eventItems[EPOLL_MAX_EVENTS];// 阻塞消息int eventCount =epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);// No longer idling.
mPolling =false;// Acquire lock.
mLock.lock();// Check for poll error.if(eventCount <0){if(errno == EINTR){gotoDone;}ALOGW("Poll failed with an unexpected error: %s",strerror(errno));
result = POLL_ERROR;gotoDone;}// Check for poll timeout.if(eventCount ==0){
result = POLL_TIMEOUT;gotoDone;}Done:;// Invoke pending message callbacks.
mNextMessageUptime = LLONG_MAX;while(mMessageEnvelopes.size()!=0){
nsecs_t now =systemTime(SYSTEM_TIME_MONOTONIC);constMessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(0);if(messageEnvelope.uptime <= now){// Remove the envelope from the list.// We keep a strong reference to the handler until the call to handleMessage// finishes. Then we drop it so that the handler can be deleted *before*// we reacquire our lock.{// obtain handler
sp<MessageHandler> handler = messageEnvelope.handler;Message message = messageEnvelope.message;
mMessageEnvelopes.removeAt(0);
mSendingMessage =true;
mLock.unlock();
#if DEBUG_POLL_AND_WAKE ||DEBUG_CALLBACKSALOGD("%p ~ pollOnce - sending message: handler=%p, what=%d",this, handler.get(), message.what);
#endif
handler->handleMessage(message);}// release handler
mLock.lock();
mSendingMessage =false;
result = POLL_CALLBACK;}else{// The last message left at the head of the queue determines the next wakeup time.
mNextMessageUptime = messageEnvelope.uptime;break;}}// Release lock.
mLock.unlock();// Invoke all response callbacks.for(size_t i =0; i < mResponses.size(); i++){Response& response = mResponses.editItemAt(i);if(response.request.ident == POLL_CALLBACK){int fd = response.request.fd;int events = response.events;void* data = response.request.data;
#if DEBUG_POLL_AND_WAKE ||DEBUG_CALLBACKSALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",this, response.request.callback.get(), fd, events, data);
#endif
// Invoke the callback. Note that the file descriptor may be closed by// the callback (and potentially even reused) before the function returns so// we need to be a little careful when removing the file descriptor afterwards.int callbackResult = response.request.callback->handleEvent(fd, events, data);if(callbackResult ==0){removeFd(fd, response.request.seq);}// Clear the callback reference in the response structure promptly because we// will not clear the response vector itself until the next poll.
response.request.callback.clear();
result = POLL_CALLBACK;}}return result;}
//回收消息publicvoidrecycle(){if(isInUse()){if(gCheckRecycle){thrownewIllegalStateException("This message cannot be recycled because it "+"is still in use.");}return;}recycleUnchecked();}//真正的回收方法voidrecycleUnchecked(){// Mark the message as in use while it remains in the recycled object pool.// Clear out all other details.
flags = FLAG_IN_USE;
what =0;
arg1 =0;
arg2 =0;
obj =null;
replyTo =null;
sendingUid = UID_NONE;
workSourceUid = UID_NONE;
when =0;
target =null;
callback =null;
data =null;synchronized(sPoolSync){if(sPoolSize < MAX_POOL_SIZE){
next = sPool;
sPool =this;
sPoolSize++;}}}//消息回收调用时机Looper.loop()方法中,可以看到真实执行回收的调用的时pollOnce()该接口用于判断当前是否处理,决定回收publicstaticvoidloop(){finalLooper me =myLooper();...for(;;){if(!loopOnce(me, ident, thresholdOverride)){return;}}}privatestaticbooleanloopOnce(finalLooper me,finallong ident,finalint thresholdOverride){Message msg = me.mQueue.next();// might blockif(msg ==null){// No message indicates that the message queue is quitting.returnfalse;}...
msg.recycleUnchecked();returntrue;}