In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "when Android threads will enter the waiting state", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "under what circumstances will Android threads enter the waiting state"?
There are two situations, one is that when there is no message in the message queue, it will put the thread into a waiting state; the other is that there is a message in the message queue, but the message specifies the time of execution, and the thread will also enter the waiting state when it has not yet reached this time.
The messages in the message queue are sorted in chronological order, which we will see later when parsing the sending of messages.
The most important thing about this function is to get the next message to be processed from the message queue, that is, the MessageQueue.next function, which implements the following in the frameworks/base/core/java/android/os/MessageQueue.java file:
[java] view plaincopypublic class MessageQueue {
.
Final Message next () {
Int pendingIdleHandlerCount =-1; / /-1 only during first iteration
Int nextPollTimeoutMillis = 0
For (;;) {
If (nextPollTimeoutMillis! = 0) {
Binder.flushPendingCommands ()
}
NativePollOnce (mPtr, nextPollTimeoutMillis)
Synchronized (this) {
/ / Try to retrieve the next message. Return if found.
Final long now = SystemClock.uptimeMillis ()
Final Message msg = mMessages
If (msg! = null) {
Final long when = msg.when
If (now > = when) {
MBlocked = false
MMessages = msg.next
Msg.next = null
If (Config.LOGV) Log.v ("MessageQueue", "Returning message:" + msg)
Return msg
} else {
NextPollTimeoutMillis = (int) Math.min (when-now, Integer.MAX_VALUE)
}
} else {
NextPollTimeoutMillis =-1
}
/ / If first time, then get the number of idlers to run.
If (pendingIdleHandlerCount < 0) {
PendingIdleHandlerCount = mIdleHandlers.size ()
}
If (pendingIdleHandlerCount = = 0) {
/ / No idle handlers to run. Loop and wait some more.
MBlocked = true
Continue
}
If (mPendingIdleHandlers = = null) {
MPendingIdleHandlers = new IdleHandler [Math.max (pendingIdleHandlerCount)
]
}
MPendingIdleHandlers = mIdleHandlers.toArray (mPendingIdleHandlers)
}
/ / Run the idle handlers.
/ / We only ever reach this code block during the first iteration.
For (int I = 0; I < pendingIdleHandlerCount; iTunes +) {
Final IdleHandler idler = mPendingIdleHandlers [I]
MPendingIdleHandlers [I] = null; / / release the reference to the handler
Boolean keep = false
Try {
Keep = idler.queueIdle ()
} catch (Throwable t) {
Log.wtf ("MessageQueue", "IdleHandler threw exception", t)
}
If (! keep) {
Synchronized (this) {
MIdleHandlers.remove (idler)
}
}
}
/ / Reset the idle handler count to 0 so we do not run them again.
PendingIdleHandlerCount = 0
/ / While calling an idle handler, a new message could have been
Livered
/ / so go back and look again for a pending message without waiting.
NextPollTimeoutMillis = 0
}
}
.
}
Execute the following statement to see if there are any messages in the current message queue:
[java] view plaincopynativePollOnce (mPtr, nextPollTimeoutMillis)
This is a JNI method. We will analyze it later. The parameter mPtr passed here points to the NativeMessageQueue object we created in the JNI layer, while the parameter nextPollTimeoutMillis indicates that if there is no message in the current message queue, when it waits, the value passed in the for loop is 0, which means no wait.
At this point, I believe you have a deeper understanding of "when Android threads will enter the waiting state". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.