MessageQueue中死循環輪詢messeag沒發生ANR的原因猜想。
messageQueue輪詢消息是否存在及要傳遞給的target對象時,為什么沒發生ANR:個人猜想,當不存在在消息時,發生了循環阻塞,就像在socket編程中,當沒有客戶端訪問時,服務器端就阻塞在accept()方法中,當有消息需要處理時:循環繼續運行,不知道對不對。
messageQueue輪詢消息是否存在及要傳遞給的target對象時,為什么沒發生ANR:個人猜想,當不存在在消息時,發生了循環阻塞,就像在socket編程中,當沒有客戶端訪問時,服務器端就阻塞在accept()方法中,當有消息需要處理時:循環繼續運行,不知道對不對。
2015-03-03
舉報
2015-12-01
? ? /**
? ? ?* Run the message queue in this thread. Be sure to call
? ? ?* {@link #quit()} to end the loop.
? ? ?*/
? ? public static void loop() {
? ? ? ? Looper me = myLooper();
? ? ? ? if (me == null) {
? ? ? ? ? ? throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
? ? ? ? }
? ? ? ? MessageQueue queue = me.mQueue;
? ? ? ??
? ? ? ? // Make sure the identity of this thread is that of the local process,
? ? ? ? // and keep track of what that identity token actually is.
? ? ? ? Binder.clearCallingIdentity();
? ? ? ? final long ident = Binder.clearCallingIdentity();
? ? ? ??
? ? ? ? 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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? long wallStart = 0;
? ? ? ? ? ? ? ? long threadStart = 0;
? ? ? ? ? ? ? ? // This must be in a local variable, in case a UI event sets the logger
? ? ? ? ? ? ? ? Printer logging = me.mLogging;
? ? ? ? ? ? ? ? if (logging != null) {
? ? ? ? ? ? ? ? ? ? logging.println(">>>>> Dispatching to " + msg.target + " " +
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.callback + ": " + msg.what);
? ? ? ? ? ? ? ? ? ? wallStart = SystemClock.currentTimeMicro();
? ? ? ? ? ? ? ? ? ? threadStart = SystemClock.currentThreadTimeMicro();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? msg.target.dispatchMessage(msg);
? ? ? ? ? ? ? ? if (logging != null) {
? ? ? ? ? ? ? ? ? ? long wallTime = SystemClock.currentTimeMicro() - wallStart;
? ? ? ? ? ? ? ? ? ? long threadTime = SystemClock.currentThreadTimeMicro() - threadStart;
? ? ? ? ? ? ? ? ? ? logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
? ? ? ? ? ? ? ? ? ? if (logging instanceof Profiler) {
? ? ? ? ? ? ? ? ? ? ? ? ((Profiler) logging).profile(msg, wallStart, wallTime,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? threadStart, threadTime);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // Make sure that during the course of dispatching the
? ? ? ? ? ? ? ? // identity of the thread wasn't corrupted.
? ? ? ? ? ? ? ? final long newIdent = Binder.clearCallingIdentity();
? ? ? ? ? ? ? ? if (ident != newIdent) {
? ? ? ? ? ? ? ? ? ? Log.wtf(TAG, "Thread identity changed from 0x"
? ? ? ? ? ? ? ? ? ? ? ? ? ? + Long.toHexString(ident) + " to 0x"
? ? ? ? ? ? ? ? ? ? ? ? ? ? + Long.toHexString(newIdent) + " while dispatching to "
? ? ? ? ? ? ? ? ? ? ? ? ? ? + msg.target.getClass().getName() + " "
? ? ? ? ? ? ? ? ? ? ? ? ? ? + msg.callback + " what=" + msg.what);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? msg.recycle();
? ? ? ? ? ? }
? ? ? ? }
? ? }
2015-09-16
2015-05-20
Looper不斷輪詢MessageQueue,當取出msg時就會執行,如果該msg執行時間過長就會發生ANR。
2015-03-31
不錯,我也是這樣想的。如果發生阻塞,就會等待資源,進程實體有三個狀態,就緒,阻塞,執行。阻塞狀態就是等待資源