Android development —— 11, thread and message processing

There are two ways to create a thread:
a. Create a thread through the constructor of the Thread class

 
 
Thread thread=new Thread(new Runnable() {
    @Override
    public void run() {
        // operation to perform
        //When the thread is started, the code in the run() method will be executed
    }
});

b. Create a thread by implementing the Runnable interface

public class MainActivity extends Activity  implements Runnable{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.main);
    }
    public  void run(){
        // operation to perform
    }
}

After the thread is
created, the thread needs to be opened before the thread can be executed later. The Thread class provides the start method to start the thread. The syntax format is as follows.
Start()
For example, there is a thread named thread. thread, you can use the following code:

Thread.start();
thread sleep
thread.sleep(long  time);
thread interruption
  Thread.interrupt();
Looper (Looper)
A thread corresponds to a Looper object, and a looper object corresponds to a MessageQueue (message queue). MessageQueue is used to store Message (message), and the messages stored in MessageQueue are executed according to the FIFO principle.
The Looper object is used to open a message loop for a thread to operate MessageQueue. By default, the message loop is not enabled for newly created threads in Android, except for the main thread. The system will automatically create a Looper object for the main thread and start the message loop. Therefore, there will be no error when applying the following code to create a Handler object in the main thread, and if the following code is applied to create a Handler object in a newly created non-main thread, an exception message will appear.
Handler handler=new Handler();
Note:
If you want to create a Handler object in a non-main thread, you first need to use the prepare() method of the Looper class to initialize a Looper object, then create and change the Handler object, and then use the loop() of the Looper class. ) method starts Looper to get and process messages from the message queue.
Looper commonly used methods:


Handler message passing mechanism

The two main functions of Handler:
a. Apply Message or Runnable to post() or sendMessage() method to send to MessageQueue. When sending, you can specify delay time, sending time and Bundle data to be carried. When the MessageQueue loops to change the Message, call the handlerMessage() method of the corresponding Handler object to process it.
b. Communicate with the main thread in the child thread, that is, communicate with the UI thread in the work.
Note: In a thread, there can only be one Looper and MessageQueue, but there can be multiple Handlers, and these Handlers can share the same Looper and MessageQueue.
Methods provided by the Handler class:

Message class (Message )

Message is stored in MessageQueue . A MessageQueue can contain multiple Message objects. Each Message object can obtain a Message object through the Message.obtain() or Handler.obtainMessage() method. Specifically, it has the following five properties


Summary: Since in Android , UI components in the main thread cannot be updated in sub-threads, Android introduces a message passing mechanism. By using Looper, Handler and Message, the function of updating UI interface in multi-threading can be easily realized, which is similar to Multithreading in JAVA is different.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325695737&siteId=291194637