如何在Android中制作多线程代码?

时间:2012-12-28 15:52:31

标签: android multithreading

我想制作一个多线程程序,下面是我的试用版。但只有thread2可以工作。如果我注释掉thread2部分,那么thread1可以工作。如何使它们同时工作?谢谢!

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText edittext1, edittext2;
    volatile boolean bThreadRun = false;

    MyThread1 myThread1;
    MyThread2 myThread2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edittext1 = (EditText) findViewById(R.id.EditText1);
        edittext2 = (EditText) findViewById(R.id.EditText2);

        edittext1.setText("this is thread1");
        edittext2.setText("this is thread2");
    }

    @Override
    protected void onStart() {
        super.onStart();
        myThread1 = new MyThread1();
        myThread1.start();
        myThread2 = new MyThread2();
        myThread2.start();
        bThreadRun = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        bThreadRun = false;
        // myThread.stop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private Handler mHandler1 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                updateEditText1();
                break;
            }
        };
    };

    private Handler mHandler2 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 2:
                updateEditText2();
                break;
            }
        };
    };

    public void updateEditText1() {
        edittext1.append("thread1\n");
    }

    public void updateEditText2() {
        edittext2.append("thread2\n");
    }

    public class MyThread1 extends Thread {
        public MyThread1() {
        }

        @Override
        public void start() {
            super.start();
        }

        @Override
        public void run() {
            while (bThreadRun) {
                try {
                    sleep(500);
                    Message message = new Message();
                    message.what = 1;
                    mHandler1.sendMessage(message);
                } catch (InterruptedException ex) {
                }
            }
        }
    }

    public class MyThread2 extends Thread {
        public MyThread2() {
        }

        @Override
        public void start() {
            super.start();
        }

        @Override
        public void run() {
            while (bThreadRun) {
                try {
                    sleep(500);
                    Message message = new Message();
                    message.what = 2;
                    mHandler2.sendMessage(message);
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

这是因为Handler实例是每个活动。所以你需要将两个线程传递给同一个Handler实例,然后才能工作。

答案 1 :(得分:1)

你应该在调用thread.start()之前'bThreadRun = true