Thread and Message Processing

Source: Internet
Author: User

During program development, for some time-consuming operations, we usually open up a separate thread for them to execute, which can reduce the waiting time as much as possible. In Android, all operations are performed in the main thread by default. This main thread is responsible for managing UI-related events, and in our own child thread, therefore, Android provides a message processing and transmission mechanism to solve this problem.

1. multithreading Common Operations

1. Create a thread.

In Android, two thread creation methods are provided. (One is to create a Thread object through the Thread class constructor, and rewrite the run () method, and the other is to implement the Runnable interface .)

Method 1:

Thread thread = new Thread (new Runnable (){

@ Override

Public void run (){

// The operation to be executed

}

})

Method 2:

Public class MainActivity extends Activity implement Runnable {

@ Override

Public void run (){

// The operation to be executed

}

}

  2. Enable the thread

  3. Thread sleep

  4. Thread interruption

The following uses an instance to implement the above operations

First, layout files:

View Code

Then modify the default Activity:

MainActivity Activity Thread thread; I; Button startButton = (Button) findViewById (R. id. button1); startButton. setOnClickListener (I = 0 thread = Thread (MainActivity .); thread. start (); Button stopButton = (Button) findViewById (R. id. button2); stopButton. setOnClickListener (thread! = Thread. interrupt (); thread = Log. I ("prompt:", "interrupt thread" (thread! = Thread. interrupt (); thread = (! I ++ Log. I ("cyclic variable :"}View Code

Run the following command.

 

2. Handler message transmission mechanism

We have introduced thread-related knowledge points above, but at this time we have not performed operations on the content on the UI interface in the newly created child thread. If the above method is used for operations, an exception is thrown. Therefore, the Handler message transmission mechanism is introduced in Android to implement UI operations in newly created threads.

First, let's understand the concept of MessageQueue (Message Queue ). In Android, a thread corresponds to a loose object, and a loose object corresponds to a MessageQueue (used to store messages ).

Next,Let's take a look at several classes: The logoff class, the Message processing class Handler, and the Message class Message.

The logoff object is used to enable a message loop for a thread and operate MessgeQueue. By default, the newly created thread in Android does not enable message loop. (Except for the main thread)

 

The Message processing class (Handler) allows you to send and process Message and Rannable objects to the MessageQueue of the thread in which they are located. (It has two main functions:1, Send the Message or Runnable application post () method or sendMessage () method to MessageQueue. You can specify the delay time, sending time, or bundle data to be carried during sending. When MessageQueue loops to this Message, it calls the handlerMessage () method of the corresponding Handler object to process it.2And communicates with the main thread in the Child thread, that is, the UI thread in the work thread .) In addition, a thread can have only one loose and MessageQueue, but there can be multiple Handler, and these Handler can share one loose and MessageQueue.

 

Messages are stored in MessageQueue. A MessageQueue can contain multiple Message objects. Each Message object can be obtained through the Messhe. obtain () method or Handler. obtainMessage () method.

 

Here we will take a simple example to understand the three:

Layout file:

View Code

Create a LooperThread class:

LooperThread Handler handler1; looper. prepare (); handler1 = Log. I ("logoff" Message m = handler1.obtainMessage (); m. what = 0x11; handler1.sendMessage (m); logoff. loop ();}View Code

In the default MainActivity:

MainActivity LooperThread thread = LooperThread (); thread. start ();}View Code

Run.

 

 

The above three categories are closely related and there are many methods. We suggest you refer to the development documentation for details:

Logoff: http://developer.android.com/intl/zh-cn/reference/android/ OS /Looper.html

Handler: http://developer.android.com/intl/zh-cn/reference/android/ OS /Handler.html

Message: http://developer.android.com/intl/zh-cn/reference/android/ OS /Message.html

In addition, there are some blogs related to source code analysis for the three of them:

Http://www.cnblogs.com/zhaoxiaowei/category/568256.html

 

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.