Analysis of Asynchronous Message Processing Mechanism and Asynchronous Message Processing Mechanism

Source: Internet
Author: User

Analysis of Asynchronous Message Processing Mechanism and Asynchronous Message Processing Mechanism

1. What is asynchronous message processing?

A: For a common thread, the thread ends after the code in the run () method is executed. The Asynchronous Message Processing thread refers to the process in which a thread enters an infinite loop after being started. Each time it is executed, a message is retrieved from the Message Queue within the thread, and calls back the corresponding message processing function. After a message is executed, the loop continues. If the message queue is empty, the thread will pause (generally we call the sleep method) until there are new messages in the message queue.

2. When to use Asynchronous Message Processing?

A: If we directly put the processing function in OnCreate or OnStart of Activity when processing download or other tasks that require long execution, the entire Activity will not respond during execution, if the time is too long, the program will be suspended. Handler puts these functions in a separate thread for execution, which is independent from Activity. Therefore, we need to start another thread to process long and time-consuming operations, but the main thread is not affected. After time-consuming operations are completed, a message is sent to the main thread, and the main thread performs corresponding processing. Handler is used for message transmission and asynchronous processing between threads.

3. Asynchronous Message Processing in android consists of four parts: Message, Handler, MessageQueue, and logoff.

A. Message:

A Message is a Message transmitted in a thread. It carries a small amount of information internally and is used to exchange data between different threads.

B. Handle:

Handle is mainly used to send and process messages. Generally, the SendMessage () method is used to send messages. After a series of processing operations, the sent messages are finally transmitted to the Handle Message () method.

C. MessageQueue

MessageQueue refers to a message queue, which is mainly used to store all messages sent by Handler. This part of the message will always exist in the message queue, waiting for processing. Each thread has only one MessageQueue object.

D. Logoff

After the loop () method in logoff is called, it enters an infinite loop. Whenever a message in MessageQueue is found, it is taken out and transmitted to handleMessage () of the Hander () method. Each thread also has a logoff object.

4. Simple Example:

public class MainActivity extends ActionBarActivity {        public static final int UPDATE_TEXT=1;        private Button button;        private TextView text;        private Handler handler=new Handler(){        public void handleMessage(Message msg){            switch (msg.what) {            case UPDATE_TEXT:                                text.setText("Nice to meet you");                                break;            default:                break;            }        }            };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                button = (Button)findViewById(R.id.change_text);        text = (TextView)findViewById(R.id.text);                ButtonListener listener=new ButtonListener();        button.setOnClickListener(listener);    }                class ButtonListener implements OnClickListener{        @Override        public void onClick(View v) {                        switch (v.getId()) {            case R.id.change_text:                                new Thread(new Runnable() {                                        @Override                    public void run() {                                                Message message=new Message();                        message.what=UPDATE_TEXT;                        handler.sendMessage(message);                                            }                }).start();                                break;            default:                break;            }                    }            }}

 

Related Article

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.