1. 程式人生 > >Android開發中執行緒的複用

Android開發中執行緒的複用

專案需要在socket程式設計中傳送指令的執行緒應該實現複用,而不是每次點選一次操作就新建一次執行緒(貌似重複建立執行緒比較耗資源),因為socket的寫操作不是阻塞方法,所以必須用一個死迴圈來保證執行緒不被結束,想到利用執行緒的等待與喚醒模擬阻塞方法的效果。即使用者傳送指令的時候首先喚醒執行緒,執行完後就進入等待狀態,如此往復。執行緒類使用單例模式,使得該執行緒物件在整個專案中使用同一個物件,即在不同的Activity中都使用同一個執行緒,實現執行緒的複用。經測試該程式碼可正確實現以上功能:

第一個Activity
Java程式碼  收藏程式碼
  1. package wlx.test.thread;  
  2. import wlx.test.R;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. /* 
  10.  * 整個專案使用一個傳送執行緒,只建立一次,利用執行緒的暫停和喚醒實現執行緒的複用
     
  11.  */
  12. publicclass ThreadActivity extends Activity {  
  13.     private Button button1 = null;  
  14.     private Button button2 = null;  
  15.     private Button button3 = null;  
  16.     private SendThread sendThread = null;  
  17.     @Override
  18.     publicvoid onCreate(Bundle savedInstanceState) {  
  19.         System.out.println("ThreadActivity--->onCreate()"
    );  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_thread);  
  22.         button1 = (Button) findViewById(R.id.btn_1);  
  23.         //listener,喚醒執行緒
  24.         button1.setOnClickListener(new OnClickListener() {  
  25.             @Override
  26.             publicvoid onClick(View v) {  
  27.                 //設定資料
  28.                 sendThread.setData(newbyte[]{1,1});  
  29.                 //喚醒執行緒
  30.                 sendThread.setNotify();  
  31.             }  
  32.         });  
  33.         button2 = (Button) findViewById(R.id.btn_2);  
  34.         //listener,進入下一個Activity
  35.         button2.setOnClickListener(new OnClickListener() {  
  36.             @Override
  37.             publicvoid onClick(View v) {  
  38.                 Intent intent = new Intent();  
  39.                 intent.setClass(ThreadActivity.this, OtherActivity.class);  
  40.                 startActivity(intent);  
  41.             }  
  42.         });  
  43.         button3 = (Button) findViewById(R.id.btn_3);  
  44.         //listener,結束執行緒
  45.         button3.setOnClickListener(new OnClickListener() {  
  46.             @Override
  47.             publicvoid onClick(View v) {  
  48.                 //設定標誌位
  49.                 sendThread.setRun(false);  
  50.                 //設定資料
  51.                 sendThread.setData(newbyte[]{2,2});  
  52.                 //喚醒執行緒
  53.                 sendThread.setNotify();  
  54.             }  
  55.         });  
  56.     }  
  57.     //專案啟動的時候就啟動傳送執行緒
  58.     privatevoid startSendThread(){  
  59.         sendThread = SendThread.getInstance();  
  60.     }  
  61.     @Override
  62.     protectedvoid onDestroy() {  
  63.         System.out.println("ThreadActivity--->onDestroy()");  
  64.         super.onDestroy();  
  65.     }  
  66.     @Override
  67.     protectedvoid onPause() {  
  68.         System.out.println("ThreadActivity--->onPause()");  
  69.         super.onPause();  
  70.     }  
  71.     @Override
  72.     protectedvoid onRestart() {  
  73.         System.out.println("ThreadActivity--->onRestart()");  
  74.         super.onRestart();  
  75.     }  
  76.     @Override
  77.     protectedvoid onResume() {  
  78.         System.out.println("ThreadActivity--->onResume()");  
  79.         startSendThread();//呼叫啟動執行緒方法
  80.         super.onResume();  
  81.     }  
  82.     @Override
  83.     protectedvoid onStart() {  
  84.         System.out.println("ThreadActivity--->onStart()");  
  85.         super.onStart();  
  86.     }  
  87.     @Override
  88.     protectedvoid onStop() {  
  89.         System.out.println("ThreadActivity--->onStop()");  
  90.         super.onStop();  
  91.     }  
  92. }  
第二個Activity
Java程式碼  收藏程式碼
  1. package wlx.test.thread;  
  2. import wlx.test.R;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. /* 
  9.  * 該Activity可以使用ThreadActivity建立的傳送資料執行緒 
  10.  */
  11. publicclass OtherActivity extends Activity {  
  12.     private Button button4 = null;  
  13.     private Button button5 = null;  
  14.     private SendThread sendThread = null;  
  15.     @Override
  16.     publicvoid onCreate(Bundle savedInstanceState) {  
  17.         System.out.println("OtherActivity--->onCreate()");  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_thread_other);  
  20.         button4 = (Button) findViewById(R.id.btn_4);  
  21.         //listener
  22.         button4.setOnClickListener(new OnClickListener() {  
  23.             @Override
  24.             publicvoid onClick(View v) {  
  25.                 //設定資料
  26.                 sendThread.setData(newbyte[]{4,4});  
  27.                 //喚醒執行緒
  28.                 sendThread.setNotify();  
  29.             }  
  30.         });  
  31.         button5 = (Button) findViewById(R.id.btn_5);  
  32.         //listener
  33.         button5.setOnClickListener(new OnClickListener() {  
  34.             @Override
  35.             publicvoid onClick(View v) {  
  36.                 //設定標誌位
  37.                 sendThread.setRun(false);  
  38.                 //設定資料
  39.                 sendThread.setData(newbyte[]{5,5});  
  40.                 //喚醒執行緒
  41.                 sendThread.setNotify();  
  42.             }  
  43.         });  
  44.     }  
  45.     //得到傳送執行緒
  46.     privatevoid getSendThread(){  
  47.         sendThread = SendThread.getInstance();  
  48.     }  
  49.     @Override
  50.     protectedvoid onDestroy() {  
  51.         System.out.println("OtherActivity--->onDestroy()");  
  52.         super.onDestroy();  
  53.     }  
  54.     @Override
  55.     protectedvoid onPause() {  
  56.         System.out.println("OtherActivity--->onPause()");  
  57.         super.onPause();  
  58.     }  
  59.     @Override
  60.     protectedvoid onRestart() {  
  61.         System.out.println("OtherActivity--->onRestart()");  
  62.         super.onRestart();  
  63.     }  
  64.     @Override
  65.     protectedvoid onResume() {  
  66.         System.out.println("OtherActivity--->onResume()");  
  67.         getSendThread();//得到傳送執行緒
  68.         super.onResume();  
  69.     }  
  70.     @Override
  71.     protectedvoid onStart() {  
  72.         System.out.println("OtherActivity--->onStart()");  
  73.         super.onStart();  
  74.     }  
  75.     @Override
  76.     protectedvoid onStop() {  
  77.         System.out.println("OtherActivity--->onStop()");  
  78.         super.onStop();  
  79.     }  
  80. }  
執行緒類
Java程式碼  收藏程式碼
  1. package wlx.test.thread;  
  2. /** 
  3.  * 傳送訊息執行緒,單例 
  4.  * @author Tracy.Lee 
  5.  * @version 2012-8-10 
  6.  */
  7. publicclass SendThread extends Thread {  
  8.     privatestatic SendThread sendThread;  
  9.     privateboolean isRun = true;//是否結束執行緒的標誌位
  10.     privatebyte[] data;//需要傳送的位元組流
  11.     // 構造方法私有化
  12.     private SendThread() {}  
  13.     // 獲得物件 
  14.     publicstatic SendThread getInstance() {  
  15.         if (sendThread == null) {  
  16.             sendThread = new SendThread();  
  17.             sendThread.start();  
  18.             System.out.println("新建了一個傳送執行緒");  
  19.         }else{  
  20.             System.out.println("使用已有的傳送執行緒");  
  21.         }  
  22.         return sendThread;  
  23.     }  
  24.     @Override
  25.     publicvoid run() {  
  26.         try {  
  27.             synchronized (this){  
  28.                 while(isRun){  
  29.                     System.out.println("執行緒開始執行");  
  30.                     wait();  
  31.                     System.out.println("執行緒被喚醒");  
  32.                     System.out.println("傳送的資料-->" + data[0] + data[1]);  
  33.                 }  
  34.             }  
  35.             System.out.println("執行緒結束");  
  36.             sendThread = null;  
  37.         } catch (InterruptedException e) {  
  38.             sendThread = null;  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42.     //喚醒執行緒
  43.     publicsynchronizedvoid setNotify() {  
  44.         notify();  
  45.     }  
  46.     publicboolean isRun() {  
  47.         return isRun;  
  48.     }  
  49.     publicvoid setRun(boolean isRun) {  
  50.         this.isRun = isRun;  
  51.     }  
  52.     publicbyte[] getData() {  
  53.         return data;  
  54.     }  
  55.     publicvoid setData(byte[] data) {  
  56.         this.data = data;  
  57.     }