Some time-consuming operations in Android, such as network requests, if not responding in a timely manner, can cause the main thread to be blocked, ANR, and greatly affect the user experience, so some time-consuming operations, we will find ways to put in the sub-thread to complete.
Android UI operations are not thread-safe, so it can cause thread-safety problems when multiple threads concurrently manipulate UI components. To solve this problem, Android only allows the UI thread to modify the UI components.
public class Mainactivity extends appcompatactivity{ TextView TextView; Button Changetext; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Changetext = (Button) Findviewbyid (R.ID.BTN); TextView = (TextView) Findviewbyid (R.id.textview); Changetext.setonclicklistener (this); } public void Change (view view) { switch (View.getid ()) { r.id.btn: new Thread (new Runnable () { @Override public void Run () { textview.settext ("changed"); } }). Start (); break; Default: Break;}}}
A button and a textview are defined in the XML, and when the button is clicked, the child thread is turned on to change the text in the TextView, but it cannot be passed at compile time, because the components in the UI thread are not allowed to operate directly in the child thread.
The
needs to borrow handler to implement the functionality of the child thread to update the UI component.
public class Mainactivity extends appcompatactivity{private TextView TextView; Private Handler Handler = new Handler () {//Received msg public void Handlemessage (message msg) { Switch (msg.what) {case 0x0001:int index = MSG.ARG1; Textview.settext (Index + ""); Break } } }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1); } public void ChangeNumber (view view) {switch (View.getid ()) {case R.ID.BTN: New Thread () {public void run () {for (int i = 0; I < 10; i++) {Message msg = new Message (); Msg.what is essential and needs to be used to make judgments msg.what = 0x0001; MSG.ARG1 = i; Handler.sendmessage (msg); try {thread.sleep (1000); } catch (Interruptedexception e) {e.printstacktrace (); }}}}.start (); Break } }}
MessageQueue Message Queuing
Android uses the MessageQueue mechanism to ensure inter-thread communication.
MessageQueue is a message queue that is used to store messages published through Handler, which, by default, creates an associated message queue for the UI thread the first time the program is launched, which is used to manage the components of the program, such as activity, Broadcastreceiver, service and so on.
Handler message delivery mechanism
Worker threads communicate through handler objects and the main thread
Handler object All work is performed in the main thread
The handler class needs to implement the Handlemessage () method to handle the message object that is taken out of the messages queue
The Handlemessage () method is called by the main thread to update the UI thread when needed, but you must ensure that this method is done quickly, because the other UI actions wait for it to finish before executing
Getting Started with Android-multithreading (i)