1. Defining a thread requires only a new class to inherit from thread. Then rewrite the run () method and write the time-consuming logic inside:
class extends thread{ publicvoid run () { // processing specific logic }}
Start with a new instance of Mythread, and then call its start () method so that the code written in run () is in the child thread:
New Mythread (). Start ();
2, more time to use the implementation of the Runnable interface way to define a thread
class Implements Runnable { publicvoid run () { // handle specific logic }}
If you use this notation, the method of starting the thread will change accordingly:
New MyThread (); New Thread (Mythread). Start ();
The thread's constructor receives a runnable parameter, Mythread is an object that implements the Runnable interface, so it is directly passed into the constructor of the thread, followed by the start () method, run () The code in will run in the child thread.
3, if you do not want to specifically define a class to implement the Runnable interface, you can also use anonymous class way:
New Thread (new Runnable () { publicvoid run () { // Handle the specific logic }). Start ();
For example: Updating the UI in a child thread,
The Android UI is thread insecure, meaning that if you want to update the UI elements in your application, you have to do it in the main thread, or you will get an unexpected event.
Verify that you first include a button and a text control in the layout.
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent">4 5 6 <Button7 Android:id= "@+id/chage_text"8 Android:layout_width= "Match_parent"9 Android:layout_height= "Wrap_content"Ten Android:text= "Change Text" /> One A <TextView - Android:id= "@+id/textview" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" - android:layout_centerinparent= "true" - Android:text= "This is a piece of text" - android:textsize= "20SP"/> + </Relativelayout>
Then the Java code:
1 Public classMainactivityextendsappcompatactivity {2 @Override3 protected voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_main);6 7 //registering buttons and text controls8Button Changetext =(Button) Findviewbyid (r.id.chage_text);9 FinalTextView TextView =(TextView) Findviewbyid (R.id.textview);Ten One //Button Response AChangetext.setonclicklistener (NewView.onclicklistener () { - @Override - Public voidOnClick (view view) { the - //turn on a child thread - NewThread (NewRunnable () { - @Override + Public voidrun () { - + //to modify text in a text control ATextview.settext ("This is another piece of text"); at } - }). Start (); - } - }); - } -}
Then installed on the phone, and then crashed, the previous said to change the UI needs to be changed in the main thread. It is necessary to use a set of asynchronous message processing mechanism, that is, the sub-thread to send a message to handler, and then handler to identify the message to do the corresponding operation.
1 Public classMainactivityextendsappcompatactivity {2 3 Public Static Final intUpdata_text = 1;4 5 PrivateTextView TextView;6 7 PrivateHandler Handler =NewHandler () {8 9 Public voidhandlemessage (Message msg) {Ten Switch(msg.what) { One CaseUpdata_text: ATextview.settext ("This is another piece of text"); - Break; - default: the Break; - } - } - }; + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); at - //registering buttons and text controls -Button Changetext =(Button) Findviewbyid (r.id.chage_text); -TextView =(TextView) Findviewbyid (R.id.textview); - - //Button Response inChangetext.setonclicklistener (NewView.onclicklistener () { - @Override to Public voidOnClick (view view) { + - //turn on a child thread the NewThread (NewRunnable () { * @Override $ Public voidrun () {Panax NotoginsengMessage message =NewMessage (); -Message.what =Updata_text; theHandler.sendmessage (message);//send the Message object out + } A }). Start (); the } + }); - } $}
Create a handler object first, and override the Handlemessage method for handling the specific message, change the UI here, and then create a message object in the child thread that specifies the What field of the message to extract the set value. Then call handler's SendMessage () method to send it out.
android--multithreaded Programming