Android Process/thread Management (i) Framework for----messaging mechanisms

Source: Internet
Author: User
<span id="Label3"></p><p><p>One: Android Processes and threads</p></p><p><p>A process is an instance of a program Running. Android's 4 main components weaken the concept of the process, especially at the app level, without the need for communication between processes.</p></p><p><p>But the nature of the program has not changed, especially the multitasking system, the basic model of the Event-driven software system is as Follows:</p></p><p><p>The entrance to the program is usually main:</p></p><p><p>1. initialize:</p></p><p><p>such as creating windows, requesting resources, and so On.</p></p><p><p>2. Enter while (true)</p></p><p><p>Handles various events in the loop until the process Exits.</p></p><p><p>The four components are part of the process carrier, the configuration process inside the androidmanifest.xml, android:process properties.</p></p><p><p>of course, all of the default is in the same process, which is configured by application and the default process is the package name of the APK.</p></p><p><p>Threads are an integral part of the process and are the basis of CPU Scheduling.</p></p><p><p>In general, there are main threads and other thread points, and only the main thread can refresh the Ui.</p></p><p><p>After the application starts, the Activitythread main thread is Created.</p></p><p><p>Components of different package names can run in the same process in a certain way.</p></p><p><p>After an activity is started, there will be at least 3 threads. A main thread and 2 binder THREADS.</p></p><p><p></p></p><p><p>Second: the Message-driven mechanism within the Android Process---handler,messagequeue,runnable,looper</p></p><p><p>1.Runnable & MessageQueue:</p></p><p><p>Runnable and message are the 2 vectors of Messages.</p></p><p><p>The behavior of a message is essentially an operation runnable, or a piece of data message containing the contents of the operation, which is handled by Handlemessage.</p></p><p><p>The way they operate is:</p></p><p><p>Public Final Boolean post (Runnable R)<br>{<br>return sendmessagedelayed (getpostmessage (r), 0);<br>}</p></p><p><p>Public final Boolean postattime (Runnable r, long Uptimemillis)</p></p><p><p>Public final Boolean postattime (Runnable r, Object token, Long Uptimemillis)</p></p><p><p>Above is the method of runnable, you can see the runnable will be divided into a message sent in the form Of.</p></p><pre><pre> <span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">Static</span> <span style="color: #000000;">Message getpostmessage (Runnable r) { </span>=<span style="color: #000000;"> message.obtain (); </span> =<span style="color: #000000;"> r; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">m; }</span></pre></pre><p><p>So in essence, it's handled in a message-wrapped way.</p></p><p><p>Eventually all the messages will be put into Messagequeue.</p></p><p><p>MessageQueue is not a real queue, but a list of links.</p></p><p><p>Looper is the thing that loops over something, something like while (true).</p></p><p><p>Handler is the real thing to do.</p></p><p><p>Looper constantly from the MessageQueue out of the data, and then handed over to handler to deal With.</p></p><p><p>2.Handler:</p></p><p><p>Framework/base/core/android/os/handler.java</p></p><p><p>In fact Handler's role, its annotations have been explained very clearly.</p></p><pre><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* A Handler allows you to send and process {</span></span><span style="color: #808080;"><span style="color: #808080;">@link</span></span><span style="color: #008000;"><span style="color: #008000;">Message} and Runnable * objects associated with a thread ' s {</span></span><span style="color: #808080;"><span style="color: #808080;">@link</span></span><span style="color: #008000;"><span style="color: #008000;">MessageQueue}. Each Handler * instance are associated with a single thread and that thread ' s message * Queue. When you create a new Handler, it's bound to the thread/* message queue of the the thread That's creating it-from that Point on, * it'll deliver messages and runnables to that message queue and execute * them as they come out of the MESSAG E queue.* * <p>there is both main uses for a Handler: (1) to schedule messages and * Runnables to being executed as Som E Point in the future; and (2) to Enqueue * An action to being performed on a different thread than your own.* * <p>when posting or sending to A Handler, can either * allow the item to being processed as soon as the message queue is ready * To do so, or specify a Delay before it gets processed or absolute time for * it to is Processed. The latter-implement timeouts, * ticks, and other timing-based Behavior.</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span></pre><p><p>This is a total of three paragraphs with the following effect:</p></p><p><p>1) handler is transmitted using runnable or message, stored in the MessageQueue of a Thread.</p></p><p><p>When you create a new handler, he will bind to the thread that created it.</p></p><p><p>For a thread messagequeue, and Looper only One.</p></p><p><p>2) the use of handler generally has 2 kinds of scenes.</p></p><p><p>Wish do runnable or some kind of message in the Future.</p></p><p><p>or pass an action (Runnable or Message) to another thread.</p></p><p><p>A common operation is to use the main thread handler in a worker thread to manipulate the Ui.</p></p><p><p>3) you can let handler directly manipulate the message content, or wait for a period of time, this time is Configurable.</p></p><p><p>2 Features of handle</p></p><p><p>Handling message:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">dispatchmessage (message msg) Distributing Messages</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> handlemessage (message msg) handles messages, which are typically inherited by Subclasses.</pre></pre><p><p>The Looper.loop () method calls DispatchMessage to process the Message.</p></p><pre><pre> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">dispatchmessage (Message msg) { </span><span style="color: #0000ff;">if</span><span style="color: #0000ff;">null</span><span style="color: #000000;">) { handlecallback (msg); </span> <span style="color: #0000ff;">Else</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">if</span><span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> <span style="color: #000000;"> (mcallback.handlemessage ( Msg) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } } Handlemessage (msg); } }</span></pre></pre><p><p>Subclasses of handler by overloading this method, you can modify the way handler messages are Distributed.</p></p><p><p>The second function of handler is to load the message & runnable into Messagequeue.</p></p><p><p>What is the purpose of Handler,messagequeue,looper is to start the message Mechanism.</p></p><p><p>MessageQueue:</p></p><p><p>MessageQueue from where to get, from the handler source to see, is from Looper inside.</p></p><pre><pre> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Boolean</span> <span style="color: #000000;">Async) { </span>=<span style="color: #000000;"> looper; <strong><span style="color: #ff0000;"></span></strong> </span> <strong><span style="color: #ff0000;">=</span></strong> <span style="color: #000000;"> <strong><span style="color: #ff0000;"> looper.mqueue</span></strong> ; </span> =<span style="color: #000000;"> callback; </span> =<span style="color: #000000;"> async; }</span></pre></pre><p><p>Looper:</p></p><pre><pre> <span style="color: #0000ff;">Private</span> Looper (<span style="color: #0000ff;">boolean</span> <span style="color: #000000;"> quitallowed) { </span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> MessageQueue (quitallowed); </span> =<span style="color: #000000;"> Thread.CurrentThread (); }</span></pre></pre><p><p>The Looper constructor has done 2 things.</p></p><p><p>Create messagequeue, so each looper has a unique MessageQueue corresponding to it.</p></p><p><p>Get Run Thread.</p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">Sthreadlocal.get () would return null unless you ' ve called Prepare ().</span> <span style="color: #0000ff;">Static</span> <span style="color: #0000ff;">Final</span> <span style="color: #0000ff;">New</span> Threadlocal<looper> ();</pre></pre><p><p>Looper has a special variable, ThreadLocal, that the object is only global to its own thread, and the other threads cannot see it.</p></p><p><p>Looper provides a lot of static methods, so there must be some way to identify "identity".</p></p><p><p>The most important of these methods when we use Looper are the following 2:</p></p><pre><pre> <span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">Static</span> <span style="color: #0000ff;">void</span> Prepare (<span style="color: #0000ff;">boolean</span> <span style="color: #000000;"> quitallowed) { </span><span style="color: #0000ff;">if</span><span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span> <span style="color: #0000ff;"> Throw</span><span style="color: #0000ff;">New</span> runtimeexception ("only one Looper could be created per thread"<span style="color: #000000;">); } <strong>Sthreadlocal.set (</strong> </span> <strong><span style="color: #0000ff;">New</span></strong> <span style="color: #000000;"> <strong>Looper (quitallowed));</strong> }</span></pre></pre><pre><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Run The message queue in this Thread. Be sure to call * {</span></span><span style="color: #808080;"><span style="color: #808080;">@link</span></span><span style="color: #008000;"><span style="color: #008000;">#quit ()} to end of the Loop. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Loop () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Looper me =<span style="color: #000000;"><span style="color: #000000;">Mylooper (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(me = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>RuntimeException ("No Looper; Looper.prepare () wasn ' t called on the This Thread. "<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>MessageQueue queue =<span style="color: #000000;"><span style="color: #000000;">me.mqueue; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">make <span style="color: #008000;">sure the identity of the the the the The local process,</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">And keep track of the what, <span style="color: #008000;">identity token actually is.</span></span><span style="color: #000000;"><span style="color: #000000;">binder.clearcallingidentity (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span>Ident =<span style="color: #000000;"><span style="color: #000000;">binder.clearcallingidentity (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;"> (;;) {Message msg</span></span>= Queue.next ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">might block</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(msg = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">No message indicates that the message queue is Quitting.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">must is in a local variable with case a UI event sets the logger</span></span>Printer logging =<span style="color: #000000;"><span style="color: #000000;">me.mlogging; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(logging! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {logging.println (</span></span>">>>>> dispatching to" + msg.target + "" +<span style="color: #000000;"><span style="color: #000000;">Msg.callback</span></span>+ ": " +<span style="color: #000000;"><span style="color: #000000;">msg.what); } msg.target.dispatchMessage (msg); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(logging! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {logging.println (</span></span>"<<<<< finished to" + msg.target + "" +<span style="color: #000000;"><span style="color: #000000;">msg.callback); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">make <span style="color: #008000;">sure that during the course of dispatching</span> the</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The identity of the <span style="color: #008000;">thread wasn ' t Corrupted.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span>Newident =<span style="color: #000000;"><span style="color: #000000;">binder.clearcallingidentity (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(ident! =<span style="color: #000000;"><span style="color: #000000;">Newident) {LOG.WTF (TAG,</span></span>"Thread identity changed from 0x" + long.tohexstring (ident) + "to 0x" + Lon G.tohexstring (newident) + "while dispatching to" + msg.target.getClass (). getName () + "" + Msg.callback + "what=" +<span style="color: #000000;"><span style="color: #000000;">msg.what); } msg.recycle (); } }</span></span></pre><p><p>Prepare is where looper is created and bound to Thread.</p></p><p><p>The Looper.loop () method is where the entire looper mechanism Starts.</p></p><p><p>This thread will accept the message and process the Message.</p></p><p><p>Here's a little Problem:</p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">might block</span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">) { </span><span style="color: #008000;">//</span> <span style="color: #008000;"> No message indicates that the message queue is Quitting.</span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">; }</span></pre></pre><p><p>At first, MessageQueue Handler did not pass the message into the queue, supposedly the message was null, so that the Looper directly Exited.</p></p><p><p>This problem waits for the analysis source time, in Solves.</p></p><p><p>This handler,messaqequeue,looper, and thread are all Connected.</p></p><p><p>Here's another mainlooper Question.</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Static</span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">main (string[] Args) { ... Looper.preparemainlooper (); </span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">) { </span>=<span style="color: #000000;"> thread.gethandler (); } Looper.loop ();}</span></pre></pre><p><p>The above is the activitythread part of the entry function main source code:</p></p><p><p>The method of visible Preparemainlooper () is used for the main thread.</p></p><p><p>and Looper over There.</p></p><pre><pre><span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">Static</span> Looper smainlooper; <span style="color: #008000;">//</span> <span style="color: #008000;">guarded by Looper.class</span></pre></pre><p><p>To be used for other THREADS.</p></p><p><p>This allows other threads to send messages to the main thread.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>: The looper of the main thread will be used by Smainlooper as an application, saved in the static space, and other worker threads can access it</p></p><p><p>So far, the framework of the entire messaging mechanism has been driven.</p></p><p><p>Android Process/thread Management (i) Framework for----messaging mechanisms</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.