线程类使用单例模式,使得该线程对象在整个项目中使用同一个对象,即在不同的Activity中都使用同一个线程,实现线程的复用。
经测试该代码可正确实现以上功能
代码:
第一个Activity
package wlx.test.thread; import wlx.test.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /* * 整个项目使用一个发送线程,只创建一次,利用线程的暂停和唤醒实现线程的复用 */ public class ThreadActivity extends Activity { private Button button1 = null; private Button button2 = null; private Button button3 = null; private SendThread sendThread = null; @Override public void onCreate(Bundle savedInstanceState) { System.out.println("ThreadActivity--->onCreate()"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_thread); button1 = (Button) findViewById(R.id.btn_1); //listener,唤醒线程 button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //设置数据 sendThread.setData(new byte[]{1,1}); //唤醒线程 sendThread.setNotify(); } }); button2 = (Button) findViewById(R.id.btn_2); //listener,进入下一个Activity button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(ThreadActivity.this, OtherActivity.class); startActivity(intent); } }); button3 = (Button) findViewById(R.id.btn_3); //listener,结束线程 button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //设置标志位 sendThread.setRun(false); //设置数据 sendThread.setData(new byte[]{2,2}); //唤醒线程 sendThread.setNotify(); } }); } //项目启动的时候就启动发送线程 private void startSendThread(){ sendThread = SendThread.getInstance(); } @Override protected void onDestroy() { System.out.println("ThreadActivity--->onDestroy()"); super.onDestroy(); } @Override protected void onPause() { System.out.println("ThreadActivity--->onPause()"); super.onPause(); } @Override protected void onRestart() { System.out.println("ThreadActivity--->onRestart()"); super.onRestart(); } @Override protected void onResume() { System.out.println("ThreadActivity--->onResume()"); startSendThread();//调用启动线程方法 super.onResume(); } @Override protected void onStart() { System.out.println("ThreadActivity--->onStart()"); super.onStart(); } @Override protected void onStop() { System.out.println("ThreadActivity--->onStop()"); super.onStop(); } }
2.第二个Activity
package wlx.test.thread; import wlx.test.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /* * 该Activity可以使用ThreadActivity创建的发送数据线程 */ public class OtherActivity extends Activity { private Button button4 = null; private Button button5 = null; private SendThread sendThread = null; @Override public void onCreate(Bundle savedInstanceState) { System.out.println("OtherActivity--->onCreate()"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_thread_other); button4 = (Button) findViewById(R.id.btn_4); //listener button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //设置数据 sendThread.setData(new byte[]{4,4}); //唤醒线程 sendThread.setNotify(); } }); button5 = (Button) findViewById(R.id.btn_5); //listener button5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //设置标志位 sendThread.setRun(false); //设置数据 sendThread.setData(new byte[]{5,5}); //唤醒线程 sendThread.setNotify(); } }); } //得到发送线程 private void getSendThread(){ sendThread = SendThread.getInstance(); } @Override protected void onDestroy() { System.out.println("OtherActivity--->onDestroy()"); super.onDestroy(); } @Override protected void onPause() { System.out.println("OtherActivity--->onPause()"); super.onPause(); } @Override protected void onRestart() { System.out.println("OtherActivity--->onRestart()"); super.onRestart(); } @Override protected void onResume() { System.out.println("OtherActivity--->onResume()"); getSendThread();//得到发送线程 super.onResume(); } @Override protected void onStart() { System.out.println("OtherActivity--->onStart()"); super.onStart(); } @Override protected void onStop() { System.out.println("OtherActivity--->onStop()"); super.onStop(); } }
3.线程类
package wlx.test.thread; /** * 发送消息线程,单例 * @author Tracy.Lee * @version 2012-8-10 */ public class SendThread extends Thread { private static SendThread sendThread; private boolean isRun = true;//是否结束线程的标志位 private byte[] data;//需要发送的字节流 // 构造方法私有化 private SendThread() {} // 获得对象 public static SendThread getInstance() { if (sendThread == null) { sendThread = new SendThread(); sendThread.start(); System.out.println("新建了一个发送线程"); }else{ System.out.println("使用已有的发送线程"); } return sendThread; } @Override public void run() { try { synchronized (this){ while(isRun){ System.out.println("线程开始运行"); wait(); System.out.println("线程被唤醒"); System.out.println("发送的数据-->" + data[0] + data[1]); } } System.out.println("线程结束"); sendThread = null; } catch (InterruptedException e) { sendThread = null; e.printStackTrace(); } } //唤醒线程 public synchronized void setNotify() { notify(); } public boolean isRun() { return isRun; } public void setRun(boolean isRun) { this.isRun = isRun; } public byte[] getData() { return data; } public void setData(byte[] data) { this.data = data; } }