Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Handler和Thread的關係
Johnny Sung
2013/08/22
多執行緒-
1
Thursday, August 22, 13
關於我
• I'm an Android developer.
Also learning iOS development.
• Information Management
in Shih Hsin University
• 相關作品:
• ...
Good! 愛點兒
3
Thursday, August 22, 13
世新廣播電台
4
Thursday, August 22, 13
5
Thursday, August 22, 13
6
Thursday, August 22, 13
問題
7
Thursday, August 22, 13
8
Thursday, August 22, 13
9
ANR = Activity Not Responding
http://developer.android.com/training/articles/perf-anr.html
Thursday, August 22, 13
MultiThreading 多執行緒
•Runnable
•Thread
•Handler
•Message
10
Thursday, August 22, 13
11
Thursday, August 22, 13
Runnable
12
Thursday, August 22, 13
13
Thursday, August 22, 13
class GetWebPicture implements Runnable
{
@Override
public void run()
{
// ........
}
}
14
Thursday, August 22, 13
Thread
顧名思義,它就叫執⾏行緒
15
Thursday, August 22, 13
16
Thursday, August 22, 13
Thread又有分二種
•單次型 Thread(Java原有的)
•常駐型 HandlerThread (Android特有的)
常駐型 HandlerThread留到後⾯面再說
17
Thursday, August 22, 13
18
new Thread(new Runnable()
{
@Override
public void run()
{
// ......
}
}).start();
Thursday, August 22, 13
new Thread()
{
@Override
public void run()
{
// ....
}
}.start();
19
Thursday, August 22, 13
Main Thread(UI Thread)20
Thursday, August 22, 13
Main Thread(UI Thread)20
Thursday, August 22, 13
new Thread()
{
@Override
public void run()
{
// ....
}
}.start();
Thread1
Main Thread(UI Thread)20
Thursday, August 22, 13
new Thread()
{
@Override
public void run()
{
// ....
}
}.start();
Thread1
Main Thread(UI Thread)20
Thursday, August 22, 13
new Thread()
{
@Override
public void run()
{
// ....
}
}.start();
Thread1
Main Thread(UI Thread)
runOnUiThread(new Runnabl...
new Thread()
{
@Override
public void run()
{
// ....
}
}.start();
Thread1
Main Thread(UI Thread)
runOnUiThread(new Runnabl...
HandlerThread
是⼀一種常駐型的Thread
21
Thursday, August 22, 13
mThread = new HandlerThread("name");
mThread.start();
mThreadHandler = new Handler(mThread.getLooper())
{
public void hand...
在onDestroy()之中
if (mThread != null)
mThread.quit();
寫在Service裡⾯面不錯⽤用
23
Thursday, August 22, 13
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
24
Thursday, August 22, 13
HandlerThread1
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
mThread = new HandlerThread("name");
mTh...
HandlerThread1
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
mThread = new HandlerThread("name");
mTh...
HandlerThread1
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
mThread = new HandlerThread("name");
mTh...
Handler
25
Thursday, August 22, 13
26
Thursday, August 22, 13
Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0:
// ........
Message
28
Thursday, August 22, 13
Message就是要一言以蔽之
29
Thursday, August 22, 13
用一個值 (一句話)
代表一堆事情(Runnable)
30
Thursday, August 22, 13
31
Thursday, August 22, 13
obtainMessage(int what);
obtainMessage(int what, int arg1, int arg2);
obtainMessage(int what, int arg1, int arg2, Object o...
http://developer.android.com/reference/android/os/Handler.html
其他常用的Method
33
Thursday, August 22, 13
我只想寫個小東西
Message & Handler 方式對我來說太複雜了
34
Thursday, August 22, 13
有沒有更快的方法?
35
Thursday, August 22, 13
有!
36
Thursday, August 22, 13
runOnUiThread()
37
Thursday, August 22, 13
runOnUiThread(new Runnable()
{
public void run()
{
// ......
}
});
38
Thursday, August 22, 13
讓我們看一下原始碼
39
Thursday, August 22, 13
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java
// Reference from Acti...
41
Thursday, August 22, 13
Q & A
42
Thursday, August 22, 13
Follow me on Blog
43
Thursday, August 22, 13
Follow me on Facebook
44
Thursday, August 22, 13
Upcoming SlideShare
Loading in …5
×

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

2,113 views

Published on

Published in: Technology, Education

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

  1. 1. Handler和Thread的關係 Johnny Sung 2013/08/22 多執行緒- 1 Thursday, August 22, 13
  2. 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. 3. Good! 愛點兒 3 Thursday, August 22, 13
  4. 4. 世新廣播電台 4 Thursday, August 22, 13
  5. 5. 5 Thursday, August 22, 13
  6. 6. 6 Thursday, August 22, 13
  7. 7. 問題 7 Thursday, August 22, 13
  8. 8. 8 Thursday, August 22, 13
  9. 9. 9 ANR = Activity Not Responding http://developer.android.com/training/articles/perf-anr.html Thursday, August 22, 13
  10. 10. MultiThreading 多執行緒 •Runnable •Thread •Handler •Message 10 Thursday, August 22, 13
  11. 11. 11 Thursday, August 22, 13
  12. 12. Runnable 12 Thursday, August 22, 13
  13. 13. 13 Thursday, August 22, 13
  14. 14. class GetWebPicture implements Runnable { @Override public void run() { // ........ } } 14 Thursday, August 22, 13
  15. 15. Thread 顧名思義,它就叫執⾏行緒 15 Thursday, August 22, 13
  16. 16. 16 Thursday, August 22, 13
  17. 17. Thread又有分二種 •單次型 Thread(Java原有的) •常駐型 HandlerThread (Android特有的) 常駐型 HandlerThread留到後⾯面再說 17 Thursday, August 22, 13
  18. 18. 18 new Thread(new Runnable() { @Override public void run() { // ...... } }).start(); Thursday, August 22, 13
  19. 19. new Thread() { @Override public void run() { // .... } }.start(); 19 Thursday, August 22, 13
  20. 20. Main Thread(UI Thread)20 Thursday, August 22, 13
  21. 21. Main Thread(UI Thread)20 Thursday, August 22, 13
  22. 22. new Thread() { @Override public void run() { // .... } }.start(); Thread1 Main Thread(UI Thread)20 Thursday, August 22, 13
  23. 23. new Thread() { @Override public void run() { // .... } }.start(); Thread1 Main Thread(UI Thread)20 Thursday, August 22, 13
  24. 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. 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. 26. HandlerThread 是⼀一種常駐型的Thread 21 Thursday, August 22, 13
  27. 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. 28. 在onDestroy()之中 if (mThread != null) mThread.quit(); 寫在Service裡⾯面不錯⽤用 23 Thursday, August 22, 13
  29. 29. Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() 24 Thursday, August 22, 13
  30. 30. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread = new HandlerThread("name"); mThread.start(); 24 Thursday, August 22, 13
  31. 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. 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. 33. Handler 25 Thursday, August 22, 13
  34. 34. 26 Thursday, August 22, 13
  35. 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. 36. Message 28 Thursday, August 22, 13
  37. 37. Message就是要一言以蔽之 29 Thursday, August 22, 13
  38. 38. 用一個值 (一句話) 代表一堆事情(Runnable) 30 Thursday, August 22, 13
  39. 39. 31 Thursday, August 22, 13
  40. 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. 41. http://developer.android.com/reference/android/os/Handler.html 其他常用的Method 33 Thursday, August 22, 13
  42. 42. 我只想寫個小東西 Message & Handler 方式對我來說太複雜了 34 Thursday, August 22, 13
  43. 43. 有沒有更快的方法? 35 Thursday, August 22, 13
  44. 44. 有! 36 Thursday, August 22, 13
  45. 45. runOnUiThread() 37 Thursday, August 22, 13
  46. 46. runOnUiThread(new Runnable() { public void run() { // ...... } }); 38 Thursday, August 22, 13
  47. 47. 讓我們看一下原始碼 39 Thursday, August 22, 13
  48. 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. 49. 41 Thursday, August 22, 13
  50. 50. Q & A 42 Thursday, August 22, 13
  51. 51. Follow me on Blog 43 Thursday, August 22, 13
  52. 52. Follow me on Facebook 44 Thursday, August 22, 13

×