Android multi-thread diagram handler Looper MessageQueue Message

Source: Internet
Author: User

Multithreading in Android can be implemented in many ways, as we've already talked about the more encapsulated asynchronous task (Ansynctask), which we'll look at in a more flexible way: Handler Looper MessageQueue Message.

    • Message: Used to pass information between threads, the sent message is placed in the MessageQueue of the target thread.
    • MessageQueue: Used to simplify message passing between threads, MessageQueue accepts a message from the sender and serves as the input source for the message processing side. There is only one instance per thread.
    • Handler: Used to process message. Each thread can have more than one instance depending on the business needs.
    • Looper: There is only one Looper per thread (but the worker thread does not create Looper by default), it is a loop that constantly takes the message out of MessageQueue and sends it to handler processing.
, there is only one looper instance in a thread, and one MessageQueue instance, which can have multiple handler instances. It shows how handler, MessageQueue and Looper work together.The blue part of the figure is in one thread, and the green may be in another thread. Here is a small example to show how to use, when the button first press the creation of a thread, the thread will constantly notify the interface of a progress bar and new progress, when the button is pressed again and create a new thread, the thread will advance the second progress bar forward.
Package Com.example.katahandler;import Android.app.activity;import Android.os.bundle;import Android.os.Handler; Import Android.os.message;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.progressbar;public Class Mainactivity extends Activity {Button Mbutton; ProgressBar Mprogressbar; ProgressBar mprogressbar2;static Final int progress_value1 = 1;static final int progress_value2 = 2;int Mclickcount = 0;cl MyHandler extends handler{@Overridepublic void handlemessage (Message msg) {//TODO auto-generated method stub//distinguish the kind of message Class if (Msg.what = = progress_value1) {mprogressbar.setprogress (MSG.ARG1); Super.handlemessage (msg);} else if (msg.what = = progress_value2) {mprogressbar2.setprogress (MSG.ARG1); Super.handlemessage (msg);}}} MyHandler handler; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mprogressbar = (ProgressBar) Findviewbyid (r.id.progressbar1); mProgressBar2 = (ProgressBar) Findviewbyid (r.id.progressbar2); Mbutton = (Button) Findviewbyid (R.id.button1); Mbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {/ /TODO auto-generated Method Stubmclickcount++;new Thread () {@Overridepublic void run () {int what = 0;if (mclickcount==1) WH at = Progress_value1;else if (mclickcount==2) what = progress_value2;for (int i = 0;i<100;i++) {try {sleep];} catch (I Nterruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Message msg = Handler.obtainmessage (what, i+1, 0); Handler.sendmessage (msg);} Super.run ();}}. Start ();}}); Handler = new MyHandler ();}}

    1. First, customize your own handler class MyHandler in your code, overriding the Handlermessage function to handle the received message.
    2. Click the button to create a thread instance in the event, overriding its run method.
    3. In the thread's Run method, the worker thread constantly sends a message to handler, note here that the message is not created with the new method, To use the Handler.obtainmessage method, because there is an optimization of the object pool, which prevents the memory fragmentation caused by a large number of messages.
    4. Looper is not seen here because Looper is already present in the UI thread, we do not need to operate on looper, and if our handler exists in a worker thread, we must call Looper.prepare at the appropriate location of the worker thread () and Looper.loop ().
The following results are performed:Reprint Please specify Source: Android multi-thread diagram handler Looper MessageQueue Message

Android multi-thread diagram handler Looper MessageQueue Message

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.