Android开发中线程的复用

项目需要在socket编程中发送指令的线程应该实现复用,而不是每次点击一次操作就新建一次线程(貌似重复创建线程比较耗资源),因为socket的写操作不是阻塞方法,所以必须用一个死循环来保证线程不被结束,想到利用线程的等待与唤醒模拟阻塞方法的效果。即用户发送指令的时候首先唤醒线程,执行完后就进入等待状态,如此往复。线程类使用单例模式,使得该线程对象在整个项目中使用同一个对象,即在不同的Activity中都使用同一个线程,实现线程的复用。经测试该代码可正确实现以上功能:

第一个Activity
Java代码   收藏代码
  1. package wlx.test.thread;  
  2.   
  3. import wlx.test.R;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. /* 
  11.  * 整个项目使用一个发送线程,只创建一次,利用线程的暂停和唤醒实现线程的复用 
  12.  */  
  13. public class ThreadActivity extends Activity {  
  14.     private Button button1 = null;  
  15.     private Button button2 = null;  
  16.     private Button button3 = null;  
  17.     private SendThread sendThread = null;  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         System.out.println("ThreadActivity--->onCreate()");  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_thread);  
  24.         button1 = (Button) findViewById(R.id.btn_1);  
  25.         //listener,唤醒线程  
  26.         button1.setOnClickListener(new OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 //设置数据  
  30.                 sendThread.setData(new byte[]{1,1});  
  31.                 //唤醒线程  
  32.                 sendThread.setNotify();  
  33.             }  
  34.         });  
  35.           
  36.         button2 = (Button) findViewById(R.id.btn_2);  
  37.         //listener,进入下一个Activity  
  38.         button2.setOnClickListener(new OnClickListener() {  
  39.             @Override  
  40.             public void onClick(View v) {  
  41.                 Intent intent = new Intent();  
  42.                 intent.setClass(ThreadActivity.this, OtherActivity.class);  
  43.                 startActivity(intent);  
  44.             }  
  45.         });  
  46.           
  47.         button3 = (Button) findViewById(R.id.btn_3);  
  48.         //listener,结束线程  
  49.         button3.setOnClickListener(new OnClickListener() {  
  50.             @Override  
  51.             public void onClick(View v) {  
  52.                 //设置标志位  
  53.                 sendThread.setRun(false);  
  54.                 //设置数据  
  55.                 sendThread.setData(new byte[]{2,2});  
  56.                 //唤醒线程  
  57.                 sendThread.setNotify();  
  58.             }  
  59.         });  
  60.     }  
  61.       
  62.     //项目启动的时候就启动发送线程  
  63.     private void startSendThread(){  
  64.         sendThread = SendThread.getInstance();  
  65.     }  
  66.   
  67.     @Override  
  68.     protected void onDestroy() {  
  69.         System.out.println("ThreadActivity--->onDestroy()");  
  70.         super.onDestroy();  
  71.     }  
  72.   
  73.     @Override  
  74.     protected void onPause() {  
  75.         System.out.println("ThreadActivity--->onPause()");  
  76.         super.onPause();  
  77.     }  
  78.   
  79.     @Override  
  80.     protected void onRestart() {  
  81.         System.out.println("ThreadActivity--->onRestart()");  
  82.         super.onRestart();  
  83.     }  
  84.   
  85.     @Override  
  86.     protected void onResume() {  
  87.         System.out.println("ThreadActivity--->onResume()");  
  88.         startSendThread();//调用启动线程方法  
  89.         super.onResume();  
  90.     }  
  91.   
  92.     @Override  
  93.     protected void onStart() {  
  94.         System.out.println("ThreadActivity--->onStart()");  
  95.         super.onStart();  
  96.     }  
  97.   
  98.     @Override  
  99.     protected void onStop() {  
  100.         System.out.println("ThreadActivity--->onStop()");  
  101.         super.onStop();  
  102.     }  
  103.       
  104.       
  105. }  
第二个Activity
Java代码   收藏代码
  1. package wlx.test.thread;  
  2.   
  3. import wlx.test.R;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. /* 
  10.  * 该Activity可以使用ThreadActivity创建的发送数据线程 
  11.  */  
  12. public class OtherActivity extends Activity {  
  13.     private Button button4 = null;  
  14.     private Button button5 = null;  
  15.     private SendThread sendThread = null;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         System.out.println("OtherActivity--->onCreate()");  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_thread_other);  
  22.         button4 = (Button) findViewById(R.id.btn_4);  
  23.         //listener  
  24.         button4.setOnClickListener(new OnClickListener() {  
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 //设置数据  
  28.                 sendThread.setData(new byte[]{4,4});  
  29.                 //唤醒线程  
  30.                 sendThread.setNotify();  
  31.             }  
  32.         });  
  33.         button5 = (Button) findViewById(R.id.btn_5);  
  34.         //listener  
  35.         button5.setOnClickListener(new OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 //设置标志位  
  39.                 sendThread.setRun(false);  
  40.                 //设置数据  
  41.                 sendThread.setData(new byte[]{5,5});  
  42.                 //唤醒线程  
  43.                 sendThread.setNotify();  
  44.             }  
  45.         });  
  46.     }  
  47.       
  48.     //得到发送线程  
  49.     private void getSendThread(){  
  50.         sendThread = SendThread.getInstance();  
  51.     }  
  52.       
  53.     @Override  
  54.     protected void onDestroy() {  
  55.         System.out.println("OtherActivity--->onDestroy()");  
  56.         super.onDestroy();  
  57.     }  
  58.   
  59.     @Override  
  60.     protected void onPause() {  
  61.         System.out.println("OtherActivity--->onPause()");  
  62.         super.onPause();  
  63.     }  
  64.   
  65.     @Override  
  66.     protected void onRestart() {  
  67.         System.out.println("OtherActivity--->onRestart()");  
  68.         super.onRestart();  
  69.     }  
  70.   
  71.     @Override  
  72.     protected void onResume() {  
  73.         System.out.println("OtherActivity--->onResume()");  
  74.         getSendThread();//得到发送线程  
  75.         super.onResume();  
  76.     }  
  77.   
  78.     @Override  
  79.     protected void onStart() {  
  80.         System.out.println("OtherActivity--->onStart()");  
  81.         super.onStart();  
  82.     }  
  83.   
  84.     @Override  
  85.     protected void onStop() {  
  86.         System.out.println("OtherActivity--->onStop()");  
  87.         super.onStop();  
  88.     }  
  89. }  
线程类
Java代码   收藏代码
  1. package wlx.test.thread;  
  2.   
  3. /** 
  4.  * 发送消息线程,单例 
  5.  * @author Tracy.Lee 
  6.  * @version 2012-8-10 
  7.  */  
  8. public class SendThread extends Thread {  
  9.     private static SendThread sendThread;  
  10.     private boolean isRun = true;//是否结束线程的标志位  
  11.     private byte[] data;//需要发送的字节流  
  12.       
  13.     // 构造方法私有化  
  14.     private SendThread() {}  
  15.       
  16.     // 获得对象   
  17.     public static SendThread getInstance() {  
  18.         if (sendThread == null) {  
  19.             sendThread = new SendThread();  
  20.             sendThread.start();  
  21.             System.out.println("新建了一个发送线程");  
  22.         }else{  
  23.             System.out.println("使用已有的发送线程");  
  24.         }  
  25.         return sendThread;  
  26.     }  
  27.       
  28.     @Override  
  29.     public void run() {  
  30.         try {  
  31.             synchronized (this){  
  32.                 while(isRun){  
  33.                     System.out.println("线程开始运行");  
  34.                     wait();  
  35.                     System.out.println("线程被唤醒");  
  36.                     System.out.println("发送的数据-->" + data[0] + data[1]);  
  37.                 }  
  38.             }  
  39.             System.out.println("线程结束");  
  40.             sendThread = null;  
  41.         } catch (InterruptedException e) {  
  42.             sendThread = null;  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.       
  47.     //唤醒线程  
  48.     public synchronized void setNotify() {  
  49.         notify();  
  50.     }  
  51.   
  52.     public boolean isRun() {  
  53.         return isRun;  
  54.     }  
  55.   
  56.     public void setRun(boolean isRun) {  
  57.         this.isRun = isRun;  
  58.     }  
  59.   
  60.     public byte[] getData() {  
  61.         return data;  
  62.     }  
  63.   
  64.     public void setData(byte[] data) {  
  65.         this.data = data;  
  66.     }  
  67.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值