当前位置:网站首页>Handler source code analysis 2

Handler source code analysis 2

2020-12-07 10:02:06 osc_ fqkqiook

Xiangyuan design mode

Memory reuse , Use the same memory space ,bitmap,recycleView They all use this idea

 

stay loop After the message is taken from the message queue and distributed , It doesn't destroy the message object , But through msg.recycleUnchecked() Method empty the message and put it into the message pool for recycling

This avoids creating and destroying repeatedly msg object , Cause memory jitter and oom

When creating a message, use obtain Method to retrieve from memory , There is no need to repeatedly apply for memory space

 

Synchronization barrier

Can block synchronous messages , To prioritize the execution of important asynchronous messages in the system

hit if after , Enter the loop to filter out synchronization messages , and msg.target=null When the message is a synchronization barrier

For example, screen refresh ,ANR Important messages such as notifications can be executed in this way

 

HandlerThread

HandlerThread yes Thread Subclasses of , Strictly speaking, it is a thread , It's just that it helps us create Looper

HandlerThread The meaning of existence is as follows :

1) Easy to use :a. Easy initialization ,b, Easy access to threads looper

2) Guaranteed thread safety

 

We use... In child threads Looper Need to pass through     Looper.prepare()    Looper.loop() Two steps , And it doesn't guarantee thread safety

have a look handlerThread Processing in

stay run Methods encapsulate these two methods for us

Two methods are implicitly locked

 

 

1. There can be countless

2. One , adopt threadLocal To guarantee

3. The inner class holds the object of the outer class , And the life cycle is inconsistent . The concrete is msg.target Hold on to handler object , and handler Object holds the outer class's activity object , When activity At the time of destruction ,msg No memory leaks due to destruction

4.mlooper.prepare and mlooper.loop

5. call quit, end loop loop

6.synchronized lock

7. obtain

8.ANR Is the event 5s In response to , and looper Sleep because there is no event to execute , There is no correlation between the two

版权声明
本文为[osc_ fqkqiook]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207095123827q.html