[Android] 多執行緒 - Handler和Thread的關係

[Android] 多執行緒 - Handler和Thread的關係

講解初學者搞不清楚的ANR等錯誤

8a6e58b272b266faf22d8a3b2927624f?s=128

Johnny Sung

August 22, 2013
Tweet

Transcript

  1. )BOEMFSẸ5ISFBE᧣໹๹ Johnny Sung 2013/08/22 ၞ᭸ḝ緒 1 Thursday, August 22, 13

  2. ໹ᜐᚕ • I'm an Android developer. Also learning iOS development.

    • Information Management in Shih Hsin University • 相關作品: • Good! 愛點兒 • 世新廣播電台 • 主要語⾔言: • Obj-C / Java / C# / PHP 2 Thursday, August 22, 13
  3. (PPEᛔᨬᚑ 3 Thursday, August 22, 13

  4. ᕴ᙮༃ᴶᨘᴈ 4 Thursday, August 22, 13

  5. 5 Thursday, August 22, 13

  6. 6 Thursday, August 22, 13

  7. ዧ᩼ 7 Thursday, August 22, 13

  8. 8 Thursday, August 22, 13

  9. 9 "/3"DUJWJUZ/PU3FTQPOEJOH http://developer.android.com/training/articles/perf-anr.html Thursday, August 22, 13

  10. .VMUJ5ISFBEJOHၞ᭸ḝ緒 ↟3VOOBCMF ↟5ISFBE ↟)BOEMFS ↟.FTTBHF 10 Thursday, August 22, 13

  11. 11 Thursday, August 22, 13

  12. 3VOOBCMF 12 Thursday, August 22, 13

  13. 13 Thursday, August 22, 13

  14. class GetWebPicture implements Runnable { @Override public void run() {

    // ........ } } 14 Thursday, August 22, 13
  15. 5ISFBE 顧名思義,它就叫執⾏行緒 15 Thursday, August 22, 13

  16. 16 Thursday, August 22, 13

  17. 5ISFBEᠹᢶᐠᤍ᪽ ↟ၣᮈṞ5ISFBE +BWBᡰᢶ᧣ ↟ᓝ᫳Ṟ)BOEMFS5ISFBE "OESPJEᴰᢶ᧣ 常駐型 HandlerThread留到後⾯面再說 17 Thursday, August

    22, 13
  18. 18 new Thread(new Runnable() { @Override public void run() {

    // ...... } }).start(); Thursday, August 22, 13
  19. new Thread() { @Override public void run() { // ....

    } }.start(); 19 Thursday, August 22, 13
  20. Main Thread(UI Thread) 20 Thursday, August 22, 13

  21. Main Thread(UI Thread) 20 Thursday, August 22, 13

  22. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) 20 Thursday, August 22, 13
  23. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) 20 Thursday, August 22, 13
  24. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) runOnUiThread(new Runnable() { public void run() { // ...... } }); 20 Thursday, August 22, 13
  25. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) runOnUiThread(new Runnable() { public void run() { // ...... } }); 20 Thursday, August 22, 13
  26. )BOEMFS5ISFBE 是⼀一種常駐型的Thread 21 Thursday, August 22, 13

  27. mThread = new HandlerThread("name"); mThread.start(); mThreadHandler = new Handler(mThread.getLooper()) {

    public void handleMessage(android.os.Message msg) { switch (msg.what) { case START_SERVER: // ..... break; case STOP_SERVER: // ..... break; default: break; } } }; 在onCreate()之中 22 Thursday, August 22, 13
  28. 在onDestroy()之中 if (mThread != null) mThread.quit(); 寫在Service裡⾯面不錯⽤用 23 Thursday, August

    22, 13
  29. Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() 24 Thursday, August

    22, 13
  30. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); 24 Thursday, August 22, 13
  31. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); if (mThread != null) mThread.quit(); 24 Thursday, August 22, 13
  32. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); if (mThread != null) mThread.quit(); (Idle) (Running) (Running) (Idle) (Idle) 24 Thursday, August 22, 13
  33. )BOEMFS 25 Thursday, August 22, 13

  34. 26 Thursday, August 22, 13

  35. Handler mHandler = new Handler() { @Override public void handleMessage(Message

    msg) { switch (msg.what) { case 0: // ....... break; case 1: // ....... break; case 2: // ....... break; default: break; } } }; 27 Thursday, August 22, 13
  36. .FTTBHF 28 Thursday, August 22, 13

  37. .FTTBHFᲘᙈ᠓᤼ᜡᤎᵵᬞ 29 Thursday, August 22, 13

  38. ᠭ᤼෪值 ᤼༾ề ႚᶡ᤼ᴣᑺᨼ 3VOOBCMF 30 Thursday, August 22, 13

  39. 31 Thursday, August 22, 13

  40. obtainMessage(int what); obtainMessage(int what, int arg1, int arg2); obtainMessage(int what,

    int arg1, int arg2, Object obj); obtainMessage(int what, Object obj); ፇᙺᙛ 記得加上.sendToTarget()才會發送 sendEmptyMessage(int what) 32 Thursday, August 22, 13
  41. http://developer.android.com/reference/android/os/Handler.html ࿠᳔ᓝᠭ᧣.FUIPE 33 Thursday, August 22, 13

  42. ᚕᬟᓡᒊ෪ᖀჱᔕ .FTTBHF)BOEMFSፖᙚ႞ᚕᅄᕔᴉᏝᦅᇇ 34 Thursday, August 22, 13

  43. ᢶኼᢶ๞᳓᧣ፖ᎚Ħ 35 Thursday, August 22, 13

  44. ᢶ 36 Thursday, August 22, 13

  45. SVO0O6J5ISFBE 37 Thursday, August 22, 13

  46. runOnUiThread(new Runnable() { public void run() { // ...... }

    }); 38 Thursday, August 22, 13
  47. ᜊᚕዤජ᤼᷀ᡰᘿሳ 39 Thursday, August 22, 13

  48. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java // Reference from Activity.java /** * Runs the specified

    action on the UI thread. If the current thread is the * UI thread, then the action is executed immediately. If the current thread * is not the UI thread, the action is posted to the event queue of the UI * thread. * * @param action * the action to run on the UI thread */ public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); } } 40 Thursday, August 22, 13
  49. 41 Thursday, August 22, 13

  50. 2" 42 Thursday, August 22, 13

  51. Follow me on Blog 43 Thursday, August 22, 13

  52. Follow me on Facebook 44 Thursday, August 22, 13