Android程序(Process)與線程(Thread)
當一個Android應用程式元件啟動時候,如果此時這個程式的其他元件沒有正在運作,那麼系統會為這個程式以單一線程的形式啟動一個新的
Linux程序
。預設情況下,同一應用程式下的所有元件都運作再相同的
程序
和
線程
(一般稱為程式的“主”線程)中。如果一個應用元件啟動但這個應用的程序已經存在了(因為這個應用的其他元件已經在之前啟動了),那麼這個元件将會在這個程序中啟動,同時在這個應用的主線程裡面執行。然而,你也可以讓你的應用裡面的元件運作在 不同的程序裡面,也可以為任何程序添加額外的線程。
程序(Process)
預設情況下,同一程式的所有元件都運作在相同的程序裡面,大多數的應用都是這樣的。然而,如果你發現你需要讓你的程式裡面的某個元件運作在特定的程序裡面,你可以在
manifest
檔案裡面設定。
線程(Thread)
當一個應用啟動的時候,系統會為它建立一個線程,稱為“主線程”。這個線程很重要。它負責處理排程事件到相關的 user interface widgets,包括繪制事件。你的應用也是在這個線程裡面與來自Android UI toolkit (包括來自 android.widget 和 android.view 包的元件)的元件進行互動。是以,這個主線程有時候也被稱為
UI 線程
。
Android的UI 線程不是線程安全的。是以你不能在一個
worker線程
操作你的UI——你必須在UI線程上對你的UI進行操作。這有兩條簡單的關于Android單線程模型的規則:
- 不要阻塞 UI 線程
- 不要在非UI線程裡通路 Android UI toolkit
是以,我們需要在worker線程中進行諸如
通路網絡資料
和
查詢資料庫
等這樣的耗時操作。那麼,通路到的網絡資料結果和資料庫查詢結果如何傳遞到UI線程呢?Android系統為我們提供了
Handler
和
AsyncTask
等工具來解決主線程與worker線程的互動問題。
Handler
了解Handler
原汁原味:
A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it – from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler:
1. to schedule messages and runnables to be executed as some point in the future;
2. to enqueue an action to be performed on a different thread than your own.
Scheduling messages is accomplished with the post,
postAtTime(Runnable, long)
,
postDelayed
,
sendEmptyMessage
,
sendMessage
,
sendMessageAtTime
, and
sendMessageDelayed
methods. The
post
versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the
sendMessage
versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler’s
handleMessage
method (requiring that you implement a subclass of Handler).
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same
post
or
sendMessage
methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler’s message queue and processed when appropriate.
剖析Handler
關于Handler的實作機制,網絡上有很多個版本。大家看過之後都應該能了解其是以然。但我個人更喜歡用圖表來說明問題,直覺啊!
Handler的類關系圖:

相關類說明
- Handler:發送和處理
Message
- MessageQueue:負責持有Looper的
。Handler發送的Message并非直接被添加到MessageQueue中,而是追加在Message連結清單的後面,通過Message連結清單
方法提供連結清單中的Message給Looper使用;next()
- Message : 包含對Message的描述以及可以攜帶任意的資料,并提供将這些資訊發送給Handler的方法
。另,負責連結Message持有的Handler發送的其他Message。sendToTarget()
- Looper:與Thread協同工作,與Thread是一對一關系。它負責從MessageQueue裡取出Message,并調用Message持有的Handler執行個體的
方法進行Message分發。Looper會一直執行下去,直到MessageQueue再也不能提供Message。dispatchMessage(message)
使用Handler
替代Handler
AsyncTask
替代AsyncTask
RxJava