"Android Development Art Exploration" reading notes (ix)--Four components
NO1:
The internal storage structure of Message Queuing MessageQueue is not a real queue, but rather a single-linked list of data structures to store message lists, because single-linked lists have advantages over insertions and deletions
No2:
ThreadLocal can store and provide data in different threads with no interference , and the looper of each thread can be easily obtained through ThreadLocal.
No3:
The thread defaults to no looper, and if you need to use handler, you must create looper for the thread.
No4:
We often refer to the main thread, also called the UI thread , which is activitythread, Activitythread is created when the Looper is initialized, This is also why handler can be used by default in the main thread.
NO5:
Why is the system not allowed to access the UI in a child thread?
1) Because Android UI controls are not thread-safe, concurrent access in multiple threads can cause UI controls to be in unexpected state
2) If the system has access to the UI control plus the locking mechanism has two drawbacks
21) makes the logic of UI access more complex
22) lock mechanism reduces the efficiency of UI access because the lock mechanism blocks execution of certain threads
NO6:
1) Consider using ThreadLocalWhen some data is scoped to a thread and different threads have different copies of the data. such as Looper, Activitythread, and AMs.
2) threadlocal Another usage scenario is the transfer of objects under complex logic, such as listeners. Because parameter passing can make programming worse, static variables do not have extensibility
No7:
Why are different threads accessing the same threadlocal to get the same data?
Because different threads access the same threadlocal get method, threadlocal internally extracts an array from the respective thread and then the index from the thread based on the current threadlocal To find out the corresponding value value.
No8:
Android message mechanism:
1)ThreadLocal Working principle: Set method, Put method, get method (Table[index+1]=value)
2) How Message Queuing (MessageQueue) works: Inserting enqueuemessage, reading and deleting next (an infinite loop method)
3) HowLooper Works: Constructs a method (creates a MessageQueue, saves the current thread object), Looper.prepare () (Creates a Looper), Looper.loop () (opens the message dead loop, The only way to jump out of a loop is if the next method returns null), the Quit method (terminates the loop, otherwise waits)
4) HowHandler works: SendMessage sending messages, dispatchmessage-->handlemessage processing messages
No9:
Looper. Getmainlooper can get the looper of the main thread from anywhere
No10:
When Looper's quit method is called, Looper calls MessageQueue's quit or quitsafely method to notify the message queue to exit, and when Message Queuing is marked as exited, its The next method returns null.
NO11:
Handler the process of sending a message is simply inserting a message into the message queue, and the next method of MessageQueue returns the message to Looper,looper when the message is received and begins processing. The final message is handled by Looper by handler, which means that handler's DispatchMessage method is called, and handler enters the process of processing the message.
No12:
main thread message loop : The main thread of Android is Activitythread, the main thread of the entry method is main, in the main method of the system through the Looper.preparemainlooper () To create the Looper and MessageQueue of the main thread, and to turn on the message loop of the main thread via Looper.loop (). The main thread of the handler is ActivityThread.H
NO13:
Message loop model for the main thread:
Activitythread through Applicationthread and AMS for interprocess communication, AMS will callback the binder method in Applicationthread after the Activitythread request is completed in interprocess communication. The applicationthread then sends a message to H, and when the message is received, it switches the logic in the Applicationthread to Activitythread to execute, i.e. switch to the main thread.
"Android Development Art Quest" Reading notes (10) message mechanism of--android