Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android遊戲開發實戰(十六):多線程的四種操作方式

編輯:Android 遊戲開發

為什麼遊戲開發離不開多線程

做過Android開發的人大概都踩過這個坑:當主線程被阻塞超過5秒,系統就會彈出「應用程序無響應」的提示,這就是所謂的ANR(Application Not Responding)。善用多線程能有效避開ANR,但要注意別走極端——並非線程越多越好。真正值得丟到子線程裡的,是那些明顯耗時的操作,比如訪問網絡接口返回緩慢、加載大量數據導致列表滑動卡頓、讀取體積龐大的I/O資源等等。把這些重活交給新線程,主線程才能保持流暢。

不過多線程是把雙刃劍。線程一多,數據同步問題就跟著上門,需要開發者細心處理。因此設計時應盡量讓各線程各司其職、互不干擾。Java提供了 synchronized 這個「線程鎖」的概念,可以為對象或方法上鎖,避免多個線程同時訪問同一資源引發的衝突,有興趣的讀者不妨深入了解一下。

1. Thread 與 Handler 執行多線程

Handler的核心作用,是在主線程與我們自建的子線程之間搭起通信的橋樑。下面的例子演示了這樣一個場景:點擊按鈕後開啟一個新線程,循環加載100張圖片,每加載完一張就通過Handler給UI線程發一條消息更新進度;全部加載完畢後,再通知UI顯示總耗時。Handler在這裡扮演的角色,就是那座連接兩個世界的橋。

Java代碼

  1. import java.io.InputStream;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.    
  15. public class SingleActivity extends Activity {  
  16.    
  17.     /**讀取進度**/  
  18.     public final static int LOAD_PROGRESS = 0;  
  19.    
  20.     /**標志讀取進度結束**/  
  21.     public final static int LOAD_COMPLETE = 1;  
  22.    
  23.     /** 開始加載100張圖片按鈕 **/  
  24.     Button mButton = null;  
  25.     /** 顯示內容 **/  
  26.     TextView mTextView = null;  
  27.     /** 加載圖片前的時間 **/  
  28.     Long mLoadStatr = 0L;  
  29.     /** 加載圖片後的時間 **/  
  30.     Long mLoadEnd = 0L;  
  31.    
  32.     Context mContext = null;  
  33.    
  34.     //接收傳遞過來的信息  
  35.     Handler handler = new Handler() {  
  36.     @Override  
  37.     public void handleMessage(Message msg) {  
  38.         switch (msg.what) {  
  39.         case LOAD_PROGRESS:  
  40.         mTextView.setText("當前讀取到第" + msg.arg1 + "張圖片");  
  41.         break;  
  42.         case LOAD_COMPLETE:  
  43.         mTextView.setText("讀取結束一共耗時" + msg.arg1 + "毫秒");  
  44.         break;  
  45.         }  
  46.         super.handleMessage(msg);  
  47.         }  
  48.     };  
  49.    
  50.     @Override  
  51.     protected void onCreate(Bundle savedInstanceState) {  
  52.     setContentView(R.layout.single);  
  53.     mContext = this;  
  54.    
  55.     /** 拿到button 與 TextView 對象 **/  
  56.     mButton = (Button) findViewById(R.id.button0);  
  57.     mTextView = (TextView) findViewById(R.id.textView0);  
  58.     mTextView.setText("點擊按鈕開始更新時間");  
  59.     mButton.setOnClickListener(new OnClickListener() {  
  60.         @Override  
  61.         public void onClick(View arg0) {  
  62.         //開始讀取圖片  
  63.         LoadImage();  
  64.         }  
  65.     });  
  66.    
  67.     super.onCreate(savedInstanceState);  
  68.     }  
  69.    
  70.     public void LoadImage() {  
  71.     new Thread() {  
  72.         @Override  
  73.         public void run() {  
  74.         //得到加載圖片開始的時間  
  75.         mLoadStatr = System.currentTimeMillis();  
  76.    
  77.         for (int i = 0; i < 100; i++) {  
  78.             // 這裡循環加載圖片100遍  
  79.             ReadBitMap(mContext, R.drawable.bg);  
  80.    
  81.             // 每讀取完一張圖片將進度甩給handler  
  82.             Message msg = new Message();  
  83.             msg.what = LOAD_PROGRESS;  
  84.             msg.arg1 = i + 1;  
  85.             handler.sendMessage(msg);  
  86.         }  
  87.    
  88.         //得到加載圖片結束的時間  
  89.         mLoadEnd = System.currentTimeMillis();  
  90.    
  91.         //100張圖片加載完成  
  92.         Message msg = new Message();  
  93.         msg.what = LOAD_COMPLETE;  
  94.         msg.arg1 = (int) (mLoadEnd - mLoadStatr);  
  95.         handler.sendMessage(msg);  
  96.         }  
  97.     }.start();  
  98.    
  99.     }  
  100.    
  101.     /** 
  102.      * 讀取本地資源的圖片 
  103.      * 
  104.      * @param context 
  105.      * @param resId 
  106.      * @return 
  107.      */  
  108.     public Bitmap ReadBitMap(Context context, int resId) {  
  109.     BitmapFactory.Options opt = new BitmapFactory.Options();  
  110.     opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  111.     opt.inPurgeable = true;  
  112.     opt.inInputShareable = true;  
  113.     // 獲取資源圖片  
  114.     InputStream is = context.getResources().openRawResource(resId);  
  115.     return BitmapFactory.decodeStream(is, null, opt);  
  116.     }  
  117. }  

2. TimerTask 與 Handler 實現延遲多線程

Timer 與 TimerTask 組合起來就是一個定時器,相當於開啟一個線程,每隔指定的時間間隔執行一次任務。這個定時線程也可以隨時關閉。舉個實際例子:做網游的登錄超時檢測時,用戶輸入帳號密碼點擊登錄後,我們開啟一個TimerTask,每秒檢查一次是否登錄成功,若10秒後仍未成功便提示「登錄超時」。任務完成後,就需要在檢測線程中調用 cancel() 把Timer與TimerTask關掉,避免它繼續空轉。下面的例子完整演示了這一流程。

Java代碼

  1. import java.util.Timer;  
  2. import java.util.TimerTask;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.Message;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.    
  13. public class TimerTaskActivity extends Activity {  
  14.    
  15.     /**執行Timer進度**/  
  16.     public final static int LOAD_PROGRESS = 0;  
  17.    
  18.     /**關閉Timer進度**/  
  19.     public final static int CLOSE_PROGRESS = 1;  
  20.    
  21.     /** 開始TimerTask按鈕 **/  
  22.     Button mButton0 = null;  
  23.    
  24.     /** 關閉TimerTask按鈕 **/  
  25.     Button mButton1 = null;  
  26.    
  27.     /** 顯示內容 **/  
  28.     TextView mTextView = null;  
  29.    
  30.     Context mContext = null;  
  31.    
  32.     /**Timer對象**/  
  33.     Timer mTimer = null;  
  34.    
  35.     /**TimerTask對象**/  
  36.     TimerTask mTimerTask = null;  
  37.    
  38.     /**記錄TimerID**/  
  39.     int mTimerID = 0;  
  40.    
  41.     /**接收傳遞過來的信息**/  
  42.     Handler handler = new Handler() {  
  43.     @Override  
  44.     public void handleMessage(Message msg) {  
  45.         switch (msg.what) {  
  46.         case LOAD_PROGRESS:  
  47.         mTextView.setText("當前TimerID為" + msg.arg1 );  
  48.         break;  
  49.         case CLOSE_PROGRESS:  
  50.         mTextView.setText("當前Timer已經關閉請重新開啟" );  
  51.         break;  
  52.    
  53.         }  
  54.         super.handleMessage(msg);  
  55.         }  
  56.     };  
  57.    
  58.     @Override  
  59.     protected void onCreate(Bundle savedInstanceState) {  
  60.     setContentView(R.layout.timer);  
  61.     mContext = this;  
  62.    
  63.     /** 拿到button 與 TextView 對象 **/  
  64.     mButton0 = (Button) findViewById(R.id.button0);  
  65.     mButton1 = (Button) findViewById(R.id.button1);  
  66.     mTextView = (TextView) findViewById(R.id.textView0);  
  67.     mTextView.setText("點擊按鈕開始更新時間");  
  68.    
  69.     //開始  
  70.     mButton0.setOnClickListener(new OnClickListener() {  
  71.         @Override  
  72.         public void onClick(View arg0) {  
  73.         //開始執行timer  
  74.         StartTimer();  
  75.         }  
  76.     });  
  77.    
  78.     //關閉  
  79.     mButton1.setOnClickListener(new OnClickListener() {  
  80.         @Override  
  81.         public void onClick(View arg0) {  
  82.         //停止執行timer  
  83.         CloseTimer();  
  84.         }  
  85.     });  
  86.    
  87.     super.onCreate(savedInstanceState);  
  88.     }  
  89.    
  90.     public void StartTimer() {  
  91.    
  92.     if (mTimer == null) {  
  93.         mTimerTask = new TimerTask() {  
  94.         public void run() {  
  95.             //mTimerTask與mTimer執行的前提下每過1秒進一次這裡  
  96.             mTimerID ++;  
  97.             Message msg = new Message();  
  98.             msg.what = LOAD_PROGRESS;  
  99.             msg.arg1 = (int) (mTimerID);  
  100.             handler.sendMessage(msg);  
  101.         }  
  102.         };  
  103.         mTimer = new Timer();  
  104.    
  105.         //第一個參數為執行的mTimerTask  
  106.         //第二個參數為延遲的時間 這裡寫1000的意思是mTimerTask將延遲1秒執行  
  107.         //第三個參數為多久執行一次 這裡寫1000表示每1秒執行一次mTimerTask的Run方法  
  108.         mTimer.schedule(mTimerTask, 1000, 1000);  
  109.     }  
  110.    
  111.     }  
  112.    
  113.     public void CloseTimer() {  
  114.    
  115.     //在這裡關閉mTimer 與 mTimerTask  
  116.     if (mTimer != null) {  
  117.         mTimer.cancel();  
  118.         mTimer = null;  
  119.     }  
  120.     if (mTimerTask != null) {  
  121.         mTimerTask = null;  
  122.     }  
  123.    
  124.     /**ID重置**/  
  125.     mTimerID = 0;  
  126.    
  127.     //這裡發送一條只帶what空的消息  
  128.     handler.sendEmptyMessage(CLOSE_PROGRESS);  
  129.     }  
  130. }  

3. AsyncTask 執行多線程

AsyncTask把異步流程拆成了幾個環節,執行順序一目了然:

onPreExecute()——最先執行,運行在UI線程中,適合做一些初始化準備;

doInBackground(Object... arg0)——真正的後台異步執行體,跑完後可以返回一個結果對象;

onProgressUpdate()——用來更新界面進度,需要在doInBackground中手動調用 publishProgress() 觸發;

onPostExecute(Object result)——在後台任務結束後接收最終結果。

下面的例子同樣計算加載100張圖片的耗時。就純執行效率而言,AsyncTask未必比Thread更快,但它勝在結構清晰、規整,能隨時拿到異步進度與最終結果,讓代碼可讀性更好。可以這樣理解:Thread能做的事,AsyncTask基本都能做;而AsyncTask能優雅完成的進度回調與結果返回,用Thread硬做往往很笨拙。我的建議是——大批量、需要回調的異步任務優先選AsyncTask,零散的小任務則用Thread即可。

Java代碼

  1. import java.io.InputStream;  
  2. import java.util.Timer;  
  3. import java.util.TimerTask;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.os.AsyncTask;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.    
  15. public class AsyncTaskActivity extends Activity {  
  16.    
  17.     /**執行Timer進度**/  
  18.     public final static int LOAD_PROGRESS = 0;  
  19.    
  20.     /**關閉Timer進度**/  
  21.     public final static int CLOSE_PROGRESS = 1;  
  22.    
  23.     /** 開始StartAsync按鈕 **/  
  24.     Button mButton0 = null;  
  25.    
  26.     /** 顯示內容 **/  
  27.     TextView mTextView = null;  
  28.    
  29.     Context mContext = null;  
  30.    
  31.     /**Timer對象**/  
  32.     Timer mTimer = null;  
  33.    
  34.     /**TimerTask對象**/  
  35.     TimerTask mTimerTask = null;  
  36.    
  37.     /**記錄TimerID**/  
  38.     int mTimerID = 0;  
  39.    
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.     setContentView(R.layout.async);  
  43.     mContext = this;  
  44.    
  45.     /** 拿到button 與 TextView 對象 **/  
  46.     mButton0 = (Button) findViewById(R.id.button0);  
  47.     mTextView = (TextView) findViewById(R.id.textView0);  
  48.     //開始  
  49.     mButton0.setOnClickListener(new OnClickListener() {  
  50.         @Override  
  51.         public void onClick(View arg0) {  
  52.         //開始執行StartAsync  
  53.         StartAsync();  
  54.         }  
  55.     });  
  56.    
  57.     super.onCreate(savedInstanceState);  
  58.     }  
  59.    
  60.     public void StartAsync() {  
  61.     new AsyncTask<Object, Object, Object>() {  
  62.    
  63.         @Override  
  64.         protected void onPreExecute() {  
  65.         //首先執行這個方法,它在UI線程中 可以執行一些異步操作  
  66.         mTextView.setText("開始加載進度");  
  67.         super.onPreExecute();  
  68.         }  
  69.    
  70.         @Override  
  71.         protected Object doInBackground(Object... arg0) {  
  72.         //異步後台執行 ,執行完畢可以返回出去一個結果object對象  
  73.    
  74.         //得到開始加載的時間  
  75.         Long startTime = System.currentTimeMillis();  
  76.         for (int i = 0; i < 100; i++) {  
  77.             // 這裡循環加載圖片100遍  
  78.             ReadBitMap(mContext, R.drawable.bg);  
  79.         //執行這個方法會異步調用onProgressUpdate方法,可以用來更新UI  
  80.             publishProgress(i);  
  81.         }  
  82.         //得到結束加載的時間  
  83.         Long endTime = System.currentTimeMillis();  
  84.    
  85.         //將讀取時間返回  
  86.         return endTime - startTime;  
  87.         }  
  88.    
  89.         @Override  
  90.         protected void onPostExecute(Object result) {  
  91.         //doInBackground之行結束以後在這裡可以接收到返回的結果對象  
  92.    
  93.         mTextView.setText("讀取100張圖片一共耗時" + result+ "毫秒");  
  94.    
  95.         super.onPostExecute(result);  
  96.         }  
  97.    
  98.         @Override  
  99.         protected void onProgressUpdate(Object... values) {  
  100.         //時時拿到當前的進度更新UI  
  101.    
  102.         mTextView.setText("當前加載進度" + values[0]);  
  103.             super.onProgressUpdate(values);  
  104.         }  
  105.     }.execute();//可以理解為執行 這個AsyncTask  
  106.    
  107.     }  
  108.     /** 
  109.      * 讀取本地資源的圖片 
  110.      * 
  111.      * @param context 
  112.      * @param resId 
  113.      * @return 
  114.      */  
  115.     public Bitmap ReadBitMap(Context context, int resId) {  
  116.     BitmapFactory.Options opt = new BitmapFactory.Options();  
  117.     opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  118.     opt.inPurgeable = true;  
  119.     opt.inInputShareable = true;  
  120.     // 獲取資源圖片  
  121.     InputStream is = context.getResources().openRawResource(resId);  
  122.     return BitmapFactory.decodeStream(is, null, opt);  
  123.     }  
  124. }  

4. 多線程中 Looper 的使用

Looper負責管理線程的消息隊列與消息循環。在Handler中,默認使用的是主線程的mainLooper;但如果在一個全新的子線程裡創建Handler,由於該線程沒有現成的Looper,程序就會拋出異常。這時需要手動調用 Looper.prepare() 為當前線程建立消息隊列,再調用 Looper.loop() 啟動消息循環,這個子線程才算具備了處理消息的能力。下面的例子演示了這一過程,值得仔細研讀。

Java代碼

  1. import java.io.InputStream;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Looper;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.    
  16. public class LooperActivity extends Activity {  
  17.     /** 發送消息按鈕 **/  
  18.     Button mButton = null;  
  19.     /** 加載圖片前的時間 **/  
  20.     Long mLoadStatr = 0L;  
  21.     /** 加載圖片後的時間 **/  
  22.     Long mLoadEnd = 0L;  
  23.    
  24.     Context mContext = null;  
  25.    
  26.     private Handler handler = new Handler() {  
  27.     public void handleMessage(Message msg) {  
  28.         new Thread() {  
  29.         @Override  
  30.         public void run() {  
  31.    
  32.             //如果handler不指定looper的話  
  33.             //默認為mainlooper來進行消息循環,  
  34.             //而當前是在一個新的線程中它沒有默認的looper  
  35.             //所以我們須要手動調用prepare()拿到他的loop  
  36.             //可以理解為在Thread創建Looper的消息隊列  
  37.             Looper.prepare();  
  38.    
  39.             Toast.makeText(LooperActivity.this, "收到消息",Toast.LENGTH_LONG).show();  
  40.    
  41.             //在這裡執行這個消息循環如果沒有這句  
  42.             //就好比只創建了Looper的消息隊列而  
  43.             //沒有執行這個隊列那麼上面Toast的內容是不會顯示出來的  
  44.             Looper.loop();  
  45.    
  46.             //如果沒有  
  47.             //Looper.prepare();  與 Looper.loop();  
  48.             //會拋出異常Can't create handler inside thread that has not called Looper.prepare()  
  49.             //原因是我們新起的線程中是沒有默認的looper所以須要手動調用prepare()拿到他的loop  
  50.            
  51.         }  
  52.         }.start();  
  53.         }  
  54.     };  
  55.    
  56.     @Override  
  57.     protected void onCreate(Bundle savedInstanceState) {  
  58.     setContentView(R.layout.loop);  
  59.     mContext = this;  
  60.    
  61.     /** 拿到button 與 TextView 對象 **/  
  62.     mButton = (Button) findViewById(R.id.button0);  
  63.     mButton.setOnClickListener(new OnClickListener() {  
  64.         @Override  
  65.         public void onClick(View arg0) {  
  66.         new Thread() {  
  67.         @Override  
  68.         public void run() {  
  69.    
  70.             //發送一條空的消息  
  71.             //空消息中必需帶一個what字段  
  72.             //用於在handler中接收  
  73.             //這裡暫時我先寫成0  
  74.                     handler.sendEmptyMessage(0);  
  75.    
  76.             }  
  77.         }.start();  
  78.         }  
  79.     });  
  80.    
  81.     super.onCreate(savedInstanceState);  
  82.     }  
  83.     /** 
  84.      * 讀取本地資源的圖片 
  85.      * 
  86.      * @param context 
  87.      * @param resId 
  88.      * @return 
  89.      */  
  90.     public Bitmap ReadBitMap(Context context, int resId) {  
  91.     BitmapFactory.Options opt = new BitmapFactory.Options();  
  92.     opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  93.     opt.inPurgeable = true;  
  94.     opt.inInputShareable = true;  
  95.     // 獲取資源圖片  
  96.     InputStream is = context.getResources().openRawResource(resId);  
  97.     return BitmapFactory.decodeStream(is, null, opt);  
  98.     }  
  99. }  

源碼下載地址:https://vdisk.weibo.com/s/aakLw

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved