andriod提供了 Handler 和 Looper 來滿足線程間的通信。為了研究其中線程機制的問題,寫了2個demo:
Demo1:
view plaincopy to clipboardprint?
01.package com.mp;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.os.Handler;
05.public class MyThread extends Activity {
06. private Handler handler = new Handler();
07. @Override
08. public void onCreate(Bundle savedInstanceState) {
09. super.onCreate(savedInstanceState);
10. handler.post(new MyRunnable());
11. System.out.println("Oncreate---The Thread id is :"
12. + Thread.currentThread().getId());
13. setContentView(R.layout.main);
14. }
15. private class MyRunnable implements Runnable {
16. public void run() {
17. System.out.println("Runnable---The Thread is running");
18. System.out.println("Runnable---The Thread id is :"
19. + Thread.currentThread().getId());
20. try {
21. Thread.sleep(6000);
22. } catch (InterruptedException e) {
23. // TODO Auto-generated catch block
24. e.printStackTrace();
25. }
26. }
27. }
28.}
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class MyThread extends Activity {
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在這個demo中,整個過程如下:
程序已啟動,就把MyRunnable加入到消息隊列中,android的handler是異步機制,所以在handler.post(new MyRunnable()); 之后,程序會繼續(xù)執(zhí)行,所以以后的語句會繼續(xù),這時候我們輸出Oncreate中的當(dāng)前線程ID。同時MyRunnable的run方法也在運行,一樣輸出run方法中的當(dāng)前線程ID,然后讓線程休眠6秒。
demo的結(jié)果分析:
1:控制臺的輸出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :1
2:程序啟動后6秒,我們才看到main.xml中的內(nèi)容(只有一個textview)
這2個結(jié)果都表明handler和主線程是同一個線程。如果這時候你做的是一個耗時的操作(比如下載),那么這樣是不可行的。
于是,android給我們提供了Looper這樣一個類。其實Android中每一個Thread都跟著一個Looper,Looper可以幫助Thread維護(hù)一個消息隊列.
Demo2:
view plaincopy to clipboardprint?
01.package com.mp;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.os.Handler;
05.import android.os.HandlerThread;
06.public class MyThread2 extends Activity {
07. private Handler handler = null;
08. @Override
09. public void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. HandlerThread handlerThread = new HandlerThread("myHandlerThread");
12. handlerThread.start();
13. handler = new Handler(handlerThread.getLooper());
14. handler.post(new MyRunnable());
15. System.out.println("Oncreate---The Thread id is :"
16. + Thread.currentThread().getId());
17. setContentView(R.layout.main);
18. }
19. private class MyRunnable implements Runnable {
20. public void run() {
21. System.out.println("Runnable---The Thread is running");
22. System.out.println("Runnable---The Thread id is :"
23. + Thread.currentThread().getId());
24. try {
25. Thread.sleep(6000);
26. } catch (InterruptedException e) {
27. // TODO Auto-generated catch block
28. e.printStackTrace();
29. }
30. }
31. }
32.}
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
public class MyThread2 extends Activity {
private Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HandlerThread handlerThread = new HandlerThread("myHandlerThread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在這個demo中,用到了HandlerThread,在HandlerThread對象中可以通過getLooper方法獲取一個Looper對象控制句柄,我們可以將其這個Looper對象映射到一個Handler中去來實現(xiàn)一個線程同步機制。于是就有以下結(jié)果;
1:控制臺的輸出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :10
2:程序啟動后,我們立刻看到main.xml中的內(nèi)容。
這樣就達(dá)到了多線程的結(jié)果。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。