[Coolxing Press: Reprinted to indicate the author and source. If there are any mistakes, please comment on them.]
Multithreading is hard to understand and grasp in all programming languages. I have read a lot of android multithreading materials online over the past few days. I will make some summary here.
When to use multithreading:
1. Time-consuming operations use multiple threads. If time-consuming operations are placed in the UI thread, user operations cannot be responded.
2. The blocking operation uses multithreading for the same reason as above.
3. multi-core CPU devices use multithreading to effectively improve CPU utilization.
4. multithreading is used for parallel operations.
The multithreading model in android mainly involves the following classes: logoff, Handler, MessageQueue, and Message.
The logoff class is used to create a message queue. each thread can have only one message queue. In android, the UI thread has a message queue by default, but non-UI threads do not have a message queue by default. if you want to enable message queue in a non-UI thread, you need to call logoff. prepare () method, a logoff object is created during the execution of this method, and a MessageQueue instance is created in the logoff Constructor (The logoff constructor is private, an object cannot be created outside the logoff class ). then, bind a Handler instance to the thread and call logoff. the loop () method can continuously retrieve and process messages from the message queue. logoff. the myLoop () method can obtain the logoff object of the thread. If it is null, the thread has not enabled Message Queue yet.
The Handler class is used to process messages. This class has four constructors:
1. public Handler (). the created Handler instance is bound to the Message Queue of the thread where the code is located. Therefore, make sure that the thread has enabled the message queue. Otherwise, an error occurs in the program. use this constructor to create a Handler instance. In general, we need to override the handleMessage () method of the Hanler class to call it in later message processing.
2. public Handler (Callback callback ). callback is an internal interface defined by Handler. To use this constructor to create a Handler object, you need to customize a class to implement the Callback interface and override the handleMessage () method defined in the interface. this constructor is similar to a constructor without parameters. Make sure that the message queue is enabled on the thread where the code is located. the difference is that when a message is processed later, the handleMessage () method of callback will be called, instead of the handleMssage () method of the Handler object.
3. public Handler (low.logoff ). this constructor creates a Handler instance and binds it to the thread where the logoff is located. in this case, logoff cannot be null. in this case, you also need to override the handleMessage () method of the Hanler class.
4. public Handler (lookloback, Callback callback). It can be understood in combination with 2 and 3.
The MessageQueue class is used to represent a message queue. each Message in the queue has a when field, which is used to determine when the Message should be processed. each Message in a Message queue is arranged in ascending order based on the size of the when field. The Message at the top is processed first. Therefore, the Message queue is not a strict FIFO queue.
The Message class is used to represent messages. the Message object can carry data through the arg1, arg2, obj fields and setData () fields. In addition, there are many fields. the when field determines when the Message should be processed. The target field is used to indicate which Handler object will process the Message. The next field indicates the next Message that is placed after the Message in the Message queue, if the callback field is not null, this Message encapsulates a runnable object. The "what" field indicates the code, that is, the type of the Message. everything is in the namespace of the handler. We only need to ensure that the attributes of the messages processed by the same handler are not repeated.
Push messages to Message queue: The target field of the Message object is associated with the Message queue of the thread, and the Message will be pushed to the Message Queue of the thread.
1. calling Methods Starting with send in the Handler class can push the Message object into the Message queue. Calling Methods Starting with post in the Handler class can encapsulate a runnable object in a Message object, then press it into the Message queue. In this case, the callback field of the Message to be queued is not null and the value is the runnable object. the target attribute of the Message that calls the methods of the Handler object to queue is assigned to this handler object.
2. Call the sendToTarget () method of the Message object to push itself into the Message Queue associated with its target field (that is, the handler object.
Delete unprocessed messages from the message queue:
You can call the Methods Starting with remove in the Handler object.
Retrieve and process a message from a message queue: All messages in the message queue have the target field. messages are retrieved and processed on the Thread associated with the target.
1. if the callback field of the retrieved Message object is not null, call the run () method of the callback field (the callback field type is runnable ). note that a new thread is not enabled to run the run () method, but directly runs on the Thread associated with the handler object (that is, the target field of the Message.
2. if the callback field of the retrieved Message object is null and the callback field in the Handler object is null, the Message will be processed by the handleMessage (msg) method of the Handler object. note that the callback field of the Message object is of the Runnable type, while the callback field of the Handler object is of the Callback type. The callback field of the Handler object is specified when the Handler instance is created, if not specified, this field is null. For details, see the four constructor methods of the Handler class.
3. If the callback field of the retrieved Message object is null and the callback field in the Handler object is not null, the Message will be processed by the handleMessage method of the callback field in the Handler object.
Inter-thread communication: with the above description, inter-thread communication is easy to understand. if A handler is associated with A Message queue on thread A, we can call the handler Method on thread B to push A Message to the Message queue on thread, this Message will be processed on thread.
There are also several classes related to multithreading in android, such as AsyncTask and HandlerThread, which will be summarized later.
From coolxing