Android Thread introduction and Examples

Source: Internet
Author: User

A very important mechanism in Android is thread + message. Of course, the thread is not unique to android. Next, let's briefly talk about what should be paid attention to when using the thread.

We use the simplest method to create an android thread + message example.

1. Thread + Handler

[Java]

Copy codeThe Code is as follows: package com. example. test_thread;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;

Public class MainActivity extends Activity {

TextView mTextView = null;
// Static TextView mTextView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MTextView = (TextView) findViewById (R. id. textview );
Thread th = new Thread (new Runnable (){

@ Override
Public void run (){
// TODO Auto-generated method stub
For (int I = 0; I <1000; I ++)
{
Try {
Thread. sleep (500 );
System. out. println ("Thread running:" + I + "! ");
Message msg = new Message ();
Msg. what = I;
MHandler. sendMessage (msg );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
});
Th. start ();

}
Public Handler mHandler = new Handler (){
// Public static Handler mHandler = new Handler (){

@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );

MTextView. setText (String. valueOf (msg. what ));
}

};

}

Package com. example. test_thread;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;

Public class MainActivity extends Activity {

TextView mTextView = null;
// Static TextView mTextView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MTextView = (TextView) findViewById (R. id. textview );
Thread th = new Thread (new Runnable (){

@ Override
Public void run (){
// TODO Auto-generated method stub
For (int I = 0; I <1000; I ++)
{
Try {
Thread. sleep (500 );
System. out. println ("Thread running:" + I + "! ");
Message msg = new Message ();
Msg. what = I;
MHandler. sendMessage (msg );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
});
Th. start ();

}
Public Handler mHandler = new Handler (){
// Public static Handler mHandler = new Handler (){

@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );

MTextView. setText (String. valueOf (msg. what ));
}

};

}

When we use the above method to create a thread, after entering the application, the thread starts to run. Handler receives the message and changes the TextView In the UI. Now everything works normally.

When you press to exit, the program exits, but the program process is still in the stack, so the sub-thread of the main thread, that is, the th (th_1) We define, will not exit. At this time, in the log information, we can see that system. out is still in print number

When you enter the program again, you can see that the information printed in log is double, but the UI changes in the order of the new thread (th_2 ).

Th_1 is still running, and handler_1 used by th_1 is also running, but the status of the previous Activity is already finish, so the UI this-> mFinished = true will not be changed

In fact, as long as th_1 has a reference to the previous Activity, the Activity will not be destroyed. The java mechanism is like this. This is the thread mechanism we recommend. The following describes the possible problems.

2. In the same example, we define Handler as static.

[Java]
Public static Handler mHandler = new Handler (){

Public static Handler mHandler = new Handler () {at this time, when you exit the application and re-enter, because Handler does not have a new instance, th_1 and th_2 send messages to a static Handler at the same time or point to the same memory area, then the numbers on TextView will jump back and forth.

3. You can also

It is not impossible to define Handler using static, as long as you re-instance a Handler in the onCreate () of the Activity, so that the JVM allocates another memory to the new Handler, so that the operation will be normal.

[Java]

Copy codeThe Code is as follows: package com. example. test_thread;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;

Public class MainActivity extends Activity {

Public Handler mHandler = null;
TextView mTextView = null;
// Static TextView mTextView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MTextView = (TextView) findViewById (R. id. textview );
MHandler = new TestHandler ();
Thread th = new Thread (new Runnable (){

@ Override
Public void run (){
// TODO Auto-generated method stub
For (int I = 0; I <1000; I ++)
{
Try {
Thread. sleep (500 );
System. out. println ("Thread running:" + I + "! ");
Message msg = new Message ();
Msg. what = I;
MHandler. sendMessage (msg );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
});
Th. start ();

}
Class TestHandler extends Handler
{
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );
System. out. println ("Handler running:" + msg. what + "! ");
MTextView. setText (String. valueOf (msg. what ));
}

}
}

Package com. example. test_thread;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;

Public class MainActivity extends Activity {

Public Handler mHandler = null;
TextView mTextView = null;
// Static TextView mTextView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MTextView = (TextView) findViewById (R. id. textview );
MHandler = new TestHandler ();
Thread th = new Thread (new Runnable (){

@ Override
Public void run (){
// TODO Auto-generated method stub
For (int I = 0; I <1000; I ++)
{
Try {
Thread. sleep (500 );
System. out. println ("Thread running:" + I + "! ");
Message msg = new Message ();
Msg. what = I;
MHandler. sendMessage (msg );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
});
Th. start ();

}
Class TestHandler extends Handler
{
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );
System. out. println ("Handler running:" + msg. what + "! ");
MTextView. setText (String. valueOf (msg. what ));
}

}
}

Of course, in general, the use of static variables is not recommended for Java, which is not in line with object-oriented thinking. Therefore, we recommend that you use some final values, try to use the message mechanism to solve the problem as much as possible, and the maintenance is easier.

Related Article

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.