利用DDMS 分析線程主要有如下幾個步驟:
1.將手機連接到 電腦,打開Eclipse 的DDMS 視圖。
2.在Devices視圖中選擇需要調試的應用程序,如 Contacts.
3.手機中啓動應用程序Contacts
4.在Devices 視圖的工具欄中選擇 Update Threads,在右邊視圖中選擇 Threads 選項。這個選項中會記錄應用中所有的線程的狀態,線程名稱爲 main的線程是主線程。
5.在應用程序中進行讓UI 出現卡頓的操作,在右邊的視圖中查看 main 線程,如果線程的狀態顯示 monitor,這表示 main 線程因爲 請求同步鎖而處於阻塞狀態。點擊Refresh 按鈕查看線程是因爲什麼原因而阻塞。
DDMS 中Threads 的狀態的說明running – executing application code – 正在執行應用程序,準確的來說正在處理消息。
sleeping – called Thread.sleep() – 執行了Thread.sleep() 方法,線程讓出了CPU,正在休眠。
monitor – waiting to acquire a monitor lock – 在正等待獲取一個監聽鎖,線程被阻塞。
wait – in Object.wait() – 在Object.wait() 方法中,線程被阻塞。
native – executing native code – 執行了原生代碼,這個對於 帶有消息隊列的線程是正常的狀態,表示消息隊列沒有任何消息,線程在native 代碼中進行無限循環,直到消息隊列中出現新的消息,消息隊列纔會返回Java 代碼處理消息。
vmwait – waiting on a VM resource – 正在等待一個虛擬機資源
zombie – thread is in the process of dying – 該線程已死
init – thread is initializing (you shouldn’t see this) – 線程正在初始化 (你不會看到這個)
starting – thread is about to start (you shouldn’t see this either) – 線程正在啓動中 (這個你也不會看到)