Android---recognize thread mode

Source: Internet
Author: User

The so-called thread is a sequential sequence of actions to achieve a purpose.

Inside the computer, there are a series of continuous movements at the same time. That is, multiple threads are executed in parallel (concurrently).

In the computer, if the computer has more than one CPU, each CPU can take care of a thread, so multiple threads can be at the same time. If there is only a single CPU, this CPU can take care of several threads at the same time (concurrently).

Whether it is a multi-CPU or a single CPU computer, multiple threads are executed in parallel, which increases execution efficiency.

In the Android platform is no exception, both in the Java layer or the C + + layer, often see multiple threads parallel situation.

Android takes Java's thread framework to assist in building multiple thread-parallel environments.

1, know the main thread of Android (also known as the UI thread)

    • ui thread Responsibility: Handling UI events quickly

    • in Android, the event that cares about the UI screen is an important responsibility of the UI thread, and it's the exclusive responsibility of the other child threads not to meddle with objects on the UI screen

    • Because Android wants the UI thread to be able to give users a quick response to their requirements. If the UI thread spends too much time doing the behind the scenes, and the user waits for more than 5 seconds after the UI event occurs, Android will apologize to the user.

    • When we launch an app, Android will be born with a new process ( process), and load the app into this newly-born session. Each process is born with a main thread, also known as the UI thread, at the time of its birth.

    • at the time of the birth of the process, in addition to the birth of the main thread, It also has a dedicated message queue and main Looper for the main thread.

This main looper is for the main thread to execute looper when it's OK, to make sure the main thread is alive and not dead, and to continue observing its message queue if there is any new information coming in when the Looper is executed. , the main thread will process (respond to) it as soon as possible.
  • For example, when a user presses a button on a UI screen, a UI event occurs, a message is dropped into MQ, including the onclick message, and the main thread takes the onclick message out of MQ and invokes the activity's onclick () function to handle. After processing, the main thread returns to continue the information loop, continues to monitor its MQ, and loops until the end of the main thread's life cycle.

  • The main thread is usually deleted when the process is deleted

  • There is a Looper class in android with an information loop in its object (message loop). In other words, a main thread has its own Looper object, which executes the information loop in this object when it is born.

  • Since the main thread continuously monitors MQ dynamics, any function in the program can communicate with the main thread as long as the information (represented by the object in the message Class) is dropped into the main thread's MQ.

  • In Android, you also define a handler class that, in any function of the program, can be used to handler objects to drop the message object into MQ and communicate with the main thread.

  • However, when the main thread is born a child thread, the child thread does not have its own Looper object and MQ in the default scenario. Since there is no Looper object, there is no loop of information (message loop), and once the work is done, the child thread ends.

  • Since there is no Looper object and no MQ, you cannot accept foreign message objects. The other thread will not be able to pass the information to it through MQ.

  • So what if other threads, such as the main thread, need to communicate with the child threads? The answer is: a Looper object and an MQ will be born for it.

2, the child thread lost information to the main thread

  • public class AC01 extends Activity implements Onclicklistener {private Thread t;    Private Handler H;    Private String str;        public void OnCreate (Bundle icicle) {t = new Thread (new Task ());    T.start (); } public void OnClick (View v) {switch (V.getid ()) {case 101:message m = h.obtainmessage (1,            , 1, NULL);            H.sendmessage (m);        Break            Case 102:settitle (str);        Break            Case 103:h.getlooper (). Quit ();            Finish ();        Break            }} class Task implements Runnable {public void run () {looper.prepare (); h = new Handler () {public void Handlemessage (Message msg) {str = Thread.CurrentThread (                                   ). GetName () + ", value=" + string.valueof (MSG.ARG1); }<pre name= "code" class= "Java" > Looper.loop ();
    } }}


  • Step-1: First, the OnCreate () function is executed by the main thread. The main thread continues to execute to the instruction:

    T = New Thread (newTask ()); T.start ();

  • A child thread is created and the child thread is started to execute the task's run () function, while the main thread returns to the information loop and continuously monitors the MQ dynamics.

  • Step-2: At this point, the child thread executes the instruction in the run () function:

    Looper.prepare ();

    A Looper object is created, and a message loop and MQ data structure are prepared.


  • Continue to the following:

    h = new Handler () {//.....

    }

    A handler object is created that assists in throwing information onto the MQ of the child thread.


  • then proceed to the instruction: Looper.loop ();

    The information loop is also started, and the MQ Dynamics of the child threads are continuously monitored.


  • Step-3: When the user presses the UI button, the UI event occurs, and Android throws the information for this UI event to MQ on the main thread, and the main thread executes the instruction in the onclick () function:

    Message m = h.obtainmessage (1, 1, NULL); H.sendmessage (m);

    The main thread throws the Message object (containing the integer value 33) into the MQ of the child thread by H, and then the main thread returns to its information loop (Looper), waiting for the event or information of the UI screen.


  • Step-4: The child thread sees that MQ has information and will take it out and call the Handlemessage () function:

    public void Handlemessage (Message msg) {

    str = Thread.CurrentThread (). GetName () +

    ", value=" + string.valueof (MSG.ARG1); }




  • Android---recognize thread mode

    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.