Android開發中執行緒的複用
阿新 • • 發佈:2019-01-06
專案需要在socket程式設計中傳送指令的執行緒應該實現複用,而不是每次點選一次操作就新建一次執行緒(貌似重複建立執行緒比較耗資源),因為socket的寫操作不是阻塞方法,所以必須用一個死迴圈來保證執行緒不被結束,想到利用執行緒的等待與喚醒模擬阻塞方法的效果。即使用者傳送指令的時候首先喚醒執行緒,執行完後就進入等待狀態,如此往復。執行緒類使用單例模式,使得該執行緒物件在整個專案中使用同一個物件,即在不同的Activity中都使用同一個執行緒,實現執行緒的複用。經測試該程式碼可正確實現以上功能:
第一個Activity
Java程式碼
Java程式碼
Java程式碼
第一個Activity
Java程式碼
- 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;
- /*
- * 整個專案使用一個傳送執行緒,只建立一次,利用執行緒的暫停和喚醒實現執行緒的複用
- */
- publicclass ThreadActivity extends Activity {
- private Button button1 = null;
- private Button button2 = null;
- private Button button3 = null;
- private SendThread sendThread = null;
- @Override
- publicvoid 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
- publicvoid onClick(View v) {
- //設定資料
- sendThread.setData(newbyte[]{1,1});
- //喚醒執行緒
- sendThread.setNotify();
- }
- });
- button2 = (Button) findViewById(R.id.btn_2);
- //listener,進入下一個Activity
- button2.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid 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
- publicvoid onClick(View v) {
- //設定標誌位
- sendThread.setRun(false);
- //設定資料
- sendThread.setData(newbyte[]{2,2});
- //喚醒執行緒
- sendThread.setNotify();
- }
- });
- }
- //專案啟動的時候就啟動傳送執行緒
- privatevoid startSendThread(){
- sendThread = SendThread.getInstance();
- }
- @Override
- protectedvoid onDestroy() {
- System.out.println("ThreadActivity--->onDestroy()");
- super.onDestroy();
- }
- @Override
- protectedvoid onPause() {
- System.out.println("ThreadActivity--->onPause()");
- super.onPause();
- }
- @Override
- protectedvoid onRestart() {
- System.out.println("ThreadActivity--->onRestart()");
- super.onRestart();
- }
- @Override
- protectedvoid onResume() {
- System.out.println("ThreadActivity--->onResume()");
- startSendThread();//呼叫啟動執行緒方法
- super.onResume();
- }
- @Override
- protectedvoid onStart() {
- System.out.println("ThreadActivity--->onStart()");
- super.onStart();
- }
- @Override
- protectedvoid onStop() {
- System.out.println("ThreadActivity--->onStop()");
- super.onStop();
- }
- }
Java程式碼
- 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建立的傳送資料執行緒
- */
- publicclass OtherActivity extends Activity {
- private Button button4 = null;
- private Button button5 = null;
- private SendThread sendThread = null;
- @Override
- publicvoid 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
- publicvoid onClick(View v) {
- //設定資料
- sendThread.setData(newbyte[]{4,4});
- //喚醒執行緒
- sendThread.setNotify();
- }
- });
- button5 = (Button) findViewById(R.id.btn_5);
- //listener
- button5.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- //設定標誌位
- sendThread.setRun(false);
- //設定資料
- sendThread.setData(newbyte[]{5,5});
- //喚醒執行緒
- sendThread.setNotify();
- }
- });
- }
- //得到傳送執行緒
- privatevoid getSendThread(){
- sendThread = SendThread.getInstance();
- }
- @Override
- protectedvoid onDestroy() {
- System.out.println("OtherActivity--->onDestroy()");
- super.onDestroy();
- }
- @Override
- protectedvoid onPause() {
- System.out.println("OtherActivity--->onPause()");
- super.onPause();
- }
- @Override
- protectedvoid onRestart() {
- System.out.println("OtherActivity--->onRestart()");
- super.onRestart();
- }
- @Override
- protectedvoid onResume() {
- System.out.println("OtherActivity--->onResume()");
- getSendThread();//得到傳送執行緒
- super.onResume();
- }
- @Override
- protectedvoid onStart() {
- System.out.println("OtherActivity--->onStart()");
- super.onStart();
- }
- @Override
- protectedvoid onStop() {
- System.out.println("OtherActivity--->onStop()");
- super.onStop();
- }
- }
Java程式碼
- package wlx.test.thread;
- /**
- * 傳送訊息執行緒,單例
- * @author Tracy.Lee
- * @version 2012-8-10
- */
- publicclass SendThread extends Thread {
- privatestatic SendThread sendThread;
- privateboolean isRun = true;//是否結束執行緒的標誌位
- privatebyte[] data;//需要傳送的位元組流
- // 構造方法私有化
- private SendThread() {}
- // 獲得物件
- publicstatic SendThread getInstance() {
- if (sendThread == null) {
- sendThread = new SendThread();
- sendThread.start();
- System.out.println("新建了一個傳送執行緒");
- }else{
- System.out.println("使用已有的傳送執行緒");
- }
- return sendThread;
- }
- @Override
- publicvoid 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();
- }
- }
- //喚醒執行緒
- publicsynchronizedvoid setNotify() {
- notify();
- }
- publicboolean isRun() {
- return isRun;
- }
- publicvoid setRun(boolean isRun) {
- this.isRun = isRun;
- }
- publicbyte[] getData() {
- return data;
- }
- publicvoid setData(byte[] data) {
- this.data = data;
- }
- }