[Android]Thread线程入门3--多线程
经过
[Android]Thread线程入门1 和[Android]Thread线程入门2 的学习,我们对线程有了简单的了解。在实际应用中,一般都会用到多线程。很少像前面的例子这么简单。那么如何实现多线程呢?
每一个线程对应一个TextView。当点击Start Thread按钮,四个线程开始启动。对应的TextView分别加1.
这时候,我们希望每一个TextView都有自己的数字显示。那么应该如何实现呢?请看下篇博文。
我们稍微修改一下前面的例子。假设我们要同时运行4个线程。每个线程对应一个TextView。如下图所示:
每一个线程对应一个TextView。当点击Start Thread按钮,四个线程开始启动。对应的TextView分别加1.
Layout文件如下:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Thread" />
<Button android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Thread" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow4" />
</LinearLayout>
源代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Thread" />
<Button android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Thread" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ffff0000"
android:text="0"
android:layout_marginLeft="30dip"
android:id="@+id/txtShow4" />
</LinearLayout>
public
class
TestThreadActivity
extends
Activity
{
private MyHandler mHandler = null;;
//4个TextView
private TextView mTextView1;
private TextView mTextView2;
private TextView mTextView3;
private TextView mTextView4;
private Button mButtonStart;
private Button mButtonStop;
//对应4个计数器
private int mSecond1 = 0;
private int mSecond2 = 0;
private int mSecond3 = 0;
private int mSecond4 = 0;
private boolean mStop = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new MyHandler();
//绑定TextView
mTextView1 = (TextView)findViewById(R.id.txtShow1);
mTextView2 = (TextView)findViewById(R.id.txtShow2);
mTextView3 = (TextView)findViewById(R.id.txtShow3);
mTextView4 = (TextView)findViewById(R.id.txtShow4);
mButtonStart = (Button)findViewById(R.id.btnStart);
mButtonStop = (Button)findViewById(R.id.btnStop);
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//启动多线程
startMultiThread();
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopThread();
}
});
}
private void startMultiThread()
{
//启动4个线程
new MyThread().start();
new MyThread().start();
new MyThread().start();
new MyThread().start();
}
private void stopThread()
{
mStop = true;
}
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
{
//暂且第一个textview加1,其余textview不变
mTextView1.setText(Integer.toString(mSecond1++));
mTextView2.setText(Integer.toString(mSecond2));
mTextView3.setText(Integer.toString(mSecond3));
mTextView4.setText(Integer.toString(mSecond4));
break;
}
}
}
}
private class MyThread extends Thread
{
@Override
public void run()
{
Log.i("test", "threadid=" + this.currentThread().getId());
while(!mStop)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
Message msg = new Message();
msg.what = 1;
//send message
mHandler.sendMessage(msg);
}
if(mStop)
{
this.interrupt();
}
}
}
}
点击Start Thread按钮,是不是和我们设想的一样,第一个TextView加1,其余textView的数值不变呢?private MyHandler mHandler = null;;
//4个TextView
private TextView mTextView1;
private TextView mTextView2;
private TextView mTextView3;
private TextView mTextView4;
private Button mButtonStart;
private Button mButtonStop;
//对应4个计数器
private int mSecond1 = 0;
private int mSecond2 = 0;
private int mSecond3 = 0;
private int mSecond4 = 0;
private boolean mStop = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new MyHandler();
//绑定TextView
mTextView1 = (TextView)findViewById(R.id.txtShow1);
mTextView2 = (TextView)findViewById(R.id.txtShow2);
mTextView3 = (TextView)findViewById(R.id.txtShow3);
mTextView4 = (TextView)findViewById(R.id.txtShow4);
mButtonStart = (Button)findViewById(R.id.btnStart);
mButtonStop = (Button)findViewById(R.id.btnStop);
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//启动多线程
startMultiThread();
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopThread();
}
});
}
private void startMultiThread()
{
//启动4个线程
new MyThread().start();
new MyThread().start();
new MyThread().start();
new MyThread().start();
}
private void stopThread()
{
mStop = true;
}
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
{
//暂且第一个textview加1,其余textview不变
mTextView1.setText(Integer.toString(mSecond1++));
mTextView2.setText(Integer.toString(mSecond2));
mTextView3.setText(Integer.toString(mSecond3));
mTextView4.setText(Integer.toString(mSecond4));
break;
}
}
}
}
private class MyThread extends Thread
{
@Override
public void run()
{
Log.i("test", "threadid=" + this.currentThread().getId());
while(!mStop)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
Message msg = new Message();
msg.what = 1;
//send message
mHandler.sendMessage(msg);
}
if(mStop)
{
this.interrupt();
}
}
}
}
观察上面图片,我们可以看到,数值并不是加1,明显是以加4的速度递增。为什么呢?
因此4个线程都在运行中,每个线程都对这个变量加1,所以我们就看到以4递增。那如何让变量还是以加1递增呢?我们修改代码如下:
private
class
MyHandler
extends
Handler
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
{
//mSecond ++; //second increase
//显示传递出来的数字msg.arg1.而不再自己计数
mTextView1.setText(Integer.toString(msg.arg1));
mTextView2.setText(Integer.toString(msg.arg1));
mTextView3.setText(Integer.toString(msg.arg1));
mTextView4.setText(Integer.toString(msg.arg1));
break;
}
}
}
}
private class MyThread extends Thread
{
//在线程里声明一个变量,用来计数
private int mSecond = 0;
@Override
public void run()
{
Log.i("test", "threadid=" + this.currentThread().getId());
while(!mStop)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
Log.e("test", "enter run exception:" + e.getLocalizedMessage());
e.printStackTrace();
}
//每隔1秒加1
mSecond ++;
//Log.i("test", "enter run 2");
Message msg = Message.obtain();
msg.what = 1;
msg.arg1 = mSecond; //将数值传递出去
//send message
mHandler.sendMessage(msg);
}
if(mStop)
this.interrupt();
}
}
运行后就会发现四个TextView都是以加1的速度整齐划一的变换着。
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
{
//mSecond ++; //second increase
//显示传递出来的数字msg.arg1.而不再自己计数
mTextView1.setText(Integer.toString(msg.arg1));
mTextView2.setText(Integer.toString(msg.arg1));
mTextView3.setText(Integer.toString(msg.arg1));
mTextView4.setText(Integer.toString(msg.arg1));
break;
}
}
}
}
private class MyThread extends Thread
{
//在线程里声明一个变量,用来计数
private int mSecond = 0;
@Override
public void run()
{
Log.i("test", "threadid=" + this.currentThread().getId());
while(!mStop)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
Log.e("test", "enter run exception:" + e.getLocalizedMessage());
e.printStackTrace();
}
//每隔1秒加1
mSecond ++;
//Log.i("test", "enter run 2");
Message msg = Message.obtain();
msg.what = 1;
msg.arg1 = mSecond; //将数值传递出去
//send message
mHandler.sendMessage(msg);
}
if(mStop)
this.interrupt();
}
}
这时候,我们希望每一个TextView都有自己的数字显示。那么应该如何实现呢?请看下篇博文。