Android Thread Introduction and Example _android

Source: Internet
Author: User

One of the most important mechanisms in Android is thread + messaging, which is not unique to Android, and, below, it's easy to say where you should pay attention when using threads.

We use the simplest way to build an Android thread + message Example

1.Thread + Handler

[Java]

Copy Code code 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+ "!");
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 (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+ "!");
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 (msg) {
TODO auto-generated Method Stub
Super.handlemessage (msg);

Mtextview.settext (string.valueof (msg.what));
}

};

}

When we set up the thread in the above way, after entering the application, the thread starts to run, the handler receives the message and changes the TextView in the UI.

When you press exit, the program exits, but the program process is still in the stack, so the thread's thread, which we defined as the th (th_1), does not exit, as we can see in the log information, System.out is still in the print number

When you enter the program again, you can see that the information that is printed in log double, but the UI will change according to the order of the new Thread (th_2)

At this point the th_1 is still running, and the handler_1 used by th_1 is running, except that the last activity's state is already finish, so the UI is not changed this->mfinished= true

In fact, as long as there is a reference to the previous activity in the th_1, then the activity will not be destroyed, this is the Java mechanism, this is our recommended threading mechanism, the following focus on the problems that may be encountered

2. The same is just the case where we define handler as static

[Java]
public static Handler Mhandler = new Handler () {

public static Handler Mhandler = new Handler () {At this point, the th_1 and th_2 send a message to a static Handler when the application is back on and re-enter, because the Handler does not have another instance. or point to the same area of memory, then there will be the phenomenon of the number of TextView on the back and forth

3. This can also

It is also not possible to define handler using static, as long as you re-instantiate a handler in the OnCreate () of the activity, so that the JVM allocates another chunk of memory to the new handler, which works fine.

[Java]

Copy Code code 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+ "!");
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 (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+ "!");
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 (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, Java still does not recommend the use of static variables, which itself does not conform to object-oriented thinking, so, in addition to some final values, as far as possible to use the message mechanism to solve the problem, maintenance also easier

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.