以下是android和ios對runloop的大概實現(xiàn)歌粥;
當(dāng)然android里面不叫runloop,叫l(wèi)ooper;
基本都是while循環(huán)來處理;
只是android是管理一個消息隊列爸业,有任務(wù)了就塞到隊列里,然后looper再取出來執(zhí)行亏镰;
android 實現(xiàn)
public static final void loop() {
Looper me = myLooper();//從該線程中取出對應(yīng)的looper對象
MessageQueue queue = me.mQueue;//取消息隊列對象...
while (true) {
Message msg = queue.next(); // might block取消息隊列中的一個待處理消息..
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message.
return;
}
//處理消息
msg.target.dispatchMessage(msg);
msg.recycle();
}
}
}
IOS 實現(xiàn)
這里刪掉了很多其他的代碼扯旷,只保留了大概邏輯
do {
//do something ......
//處理監(jiān)聽
__CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeTimers);
//處理source0 和 source1
__CFRunLoopDoSources0(rl, rlm, stopAfterHandle);
__CFRunLoopDoSource1(rl, rlm, rls, msg, msg->msgh_size, &reply) ;
//處理timer
__CFRunLoopDoTimers(rl, rlm, mach_absolute_time());
} while (0 == retVal);