Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Handler source code in the Android processor

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today Xiaobian to share with you what the Android source code is the relevant knowledge, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you have something to learn after reading this article, let's take a look at it.

In the thread variable ThreadLocal of the thread Thread, the Looper;Looper storing this thread creates a new message queue MessageQueue when initialized, and then the Looper enters an endless loop, waiting for the message Message to be obtained from the message queue MessageQueue (Looper is a consumer). It will block when there is no message.

The Handler in our program will use the sendMessage or post method to add a message to the MessageQueue. When the Message is added, it will record which Handler it belongs to. At the same time, according to the message.when, determine the location of the newly added Message in the Queue. There is only one current Message in the MessageQueue, the queue relationship is maintained through the prev,next in Message, and the Message is a node of a linked list.

After adding the message, the consumer Looper takes the Message and calls the dispatchMessage method of the Hander that creates the Message.

At first glance, it seems that Handler is sendMessage and handlerMessage, but there is still only one thread doing things.

But then I thought about it and realized the necessity of such a design.

Because this * thread is generally mainUI thread, if you have a task that can be divided into multiple small tasks to deal with, you do not use Handler, directly execute, maybe the system is busy with your task, and can not respond to user events in time, resulting in ANR throwing.

If you break your task into several small tasks and implement them in Handler, then the system can push your small tasks to the back and take the time to respond to user actions.

If there is a really big task, it usually requires another thread to handle it, or turn on Service.

An example of using handler in a new thread, let's analyze the source code

Java code

New Thread (new Runnable () {@ Override public void run () {Handler handler; / / 1, initialize Looper Looper.prepare (); / 2, bind handler to the Looper object of the CustomThread instance, define the method handler= new Handler () {@ Override public void handleMessage (Message msg) {}); / / 3, send the message handler.sendMessage (new Message ()); handler.post (new Runnable ()) handler.obtainMessage (1, "hello"). SendToTarget () / / 4. Start the message loop Looper.loop ();}}) .start ()

1 Java Cod

Public static final void prepare () {if (sThreadLocal.get ()! = null) {/ / there can be only one Looper object throw new RuntimeException ("Only one Looper may be created per thread") per thread;} / / if the current thread does not have a Looper, create a new one, and the constructor is private's sThreadLocal.set (new Looper ());} private Looper () {mQueue = new MessageQueue (); / / create a message queue mRun = true; mThread = Thread.currentThread () }

2 Java Cod

Public Handler () {mLooper = Looper.myLooper (); / / get the Looper of the current thread. If you throw an exception if (mLooper = = null) {throw new RuntimeException ("Can't create handler inside thread that has not called Looper.prepare ()");} mQueue = mLooper.mQueue; / / get message queue mCallback = null;}

3 Java Cod

/ / No matter which method is called, the final execution is public boolean sendMessageAtTime (Message msg, long uptimeMillis) {boolean sent = false; / / get the message queue MessageQueue queue = mQueue; if (queue! = null) {msg.target = this; / / send your own sent = queue.enqueueMessage (msg, uptimeMillis); / / add to the message queue} else {RuntimeException e = new RuntimeException (this + "sendMessageAtTime () called with no mQueue") Log.w ("Looper", e.getMessage (), e);} return sent;} final boolean enqueueMessage (Message msg, long when) {if (msg.when! = 0) {throw new AndroidRuntimeException (msg + "This message is already in use.");} if (msg.target = = null & &! mQuitAllowed) {throw new RuntimeException ("Main thread not allowed to quit");} synchronized (this) {if (mQuiting) {RuntimeException e = new RuntimeException (msg.target + "sending message to a Handler on a dead thread") Log.w ("MessageQueue", e.getMessage (), e); return false;} else if (msg.target = = null) {mQuiting = true;} msg.when = when; Message p = mMessages; / / there are no other messages before. The current message mMessages in MessageQueue is the msg if passed in (p = = null | | when = = 0 | when < p.when) {msg.next = p; mMessages = msg; this.notify () / / Wake up} else {/ / there are other messages before. Put the passed msg in the appropriate location, according to when Message prev = null; while (p! = null & & p.when 0) {Binder.flushPendingCommands (); this.wait (mMessages.when-now);}} else {Binder.flushPendingCommands (); this.wait () }} catch (InterruptedException e) {} the above is all the content of the article "what is the Handler source code in the Android processor?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report