亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android Watchdog 實現(xiàn)剖析

 更新時間:2012年11月14日 14:48:02   作者:  
Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動的;本文將詳細(xì)介紹
系統(tǒng)啟動過程圖:
 
Framework層所有的Service都是運(yùn)行在SystemServer進(jìn)程中;SystemServer進(jìn)程是由Zygote進(jìn)程創(chuàng)建。
SystemServer進(jìn)程啟動分兩個過程init1創(chuàng)建Service和進(jìn)程狀態(tài)對象;init2創(chuàng)建Framework層的Service,將其加入到ServiceManager中,最后啟動launcher;
Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動的。
下面看一下SystemServer中Watchdog這個過程。
SystemServer.java:
復(fù)制代碼 代碼如下:

public void run() {
//初始化Watchdog 傳入各個Service作為參數(shù)
Watchdog.getInstance().init(context, battery, power, alarm,
ActivityManagerService.self());
//啟動Watchdog
Watchdog.getInstance().start();
}

Watchdog類實現(xiàn)
類繼承結(jié)構(gòu):
 
看到Watchdog是一個Thread,運(yùn)行在SystemServer進(jìn)程中,單例模式;
HeartbeatHandler處理接受監(jiān)控的對象(Service),運(yùn)行在主線程中;
Monitor提供監(jiān)控接口,接受監(jiān)控對象實現(xiàn)此接口;
XXXService具體實現(xiàn)的檢測對象。
執(zhí)行流程:
 
對外接口
初始化:
復(fù)制代碼 代碼如下:

public void init(Context context, BatteryService battery,
PowerManagerService power, AlarmManagerService alarm,
ActivityManagerService activity) {
//存儲Service對象,運(yùn)行在同一個進(jìn)程中
mResolver = context.getContentResolver();
mBattery = battery; mPower = power;
mAlarm = alarm; mActivity = activity;
//注冊廣播
context.registerReceiver(new RebootReceiver(),
new IntentFilter(REBOOT_ACTION));
mRebootIntent = PendingIntent.getBroadcast(context,
, new Intent(REBOOT_ACTION), 0);
……
//開機(jī)時間
mBootTime = System.currentTimeMillis();
}

注冊監(jiān)控對象:
復(fù)制代碼 代碼如下:

public void addMonitor(Monitor monitor) {
synchronized (this) {
//將監(jiān)控對象加入到列表中
mMonitors.add(monitor);
}
}

搜索一下此函數(shù)的調(diào)用,表示被監(jiān)控;看到在如下Service中實現(xiàn)Watchdog的Monitor接口:
ActivityManagerService
InputManagerService
NetworkManagementService
PowerManagerService
WindowManagerService
都有調(diào)用:Watchdog.getInstance().addMonitor(this);
Watchdog線程執(zhí)行函數(shù):
復(fù)制代碼 代碼如下:

public void run() {
boolean waitedHalf = false;
while (true) {
//監(jiān)測完成標(biāo)志
mCompleted = false;
//發(fā)送監(jiān)測消息
mHandler.sendEmptyMessage(MONITOR);
synchronized (this) {
long timeout = TIME_TO_WAIT;
long start = SystemClock.uptimeMillis();
while (timeout > 0 && !mForceKillSystem) {
//休眠等待檢查結(jié)果
wait(timeout); // notifyAll() is called when mForceKillSystem is set
timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
}
if (mCompleted && !mForceKillSystem) {
//檢查結(jié)果OK
waitedHalf = false;
continue;
}
//在進(jìn)行檢查一次
if (!waitedHalf) {
ActivityManagerService.dumpStackTraces(true, pids, null, null,
NATIVE_STACKS_OF_INTEREST);
waitedHalf = true;
continue;
}
}
//表明監(jiān)控對象有問題
// If we got here, that means that the system is most likely hung.
// First collect stack traces from all threads of the system process.
// Then kill this process so that the system will restart.
//保存stack信息
……
// Only kill the process if the debugger is not attached.
if(!Debug.isDebuggerConnected()) {
if(SystemProperties.getInt("sys.watchdog.disabled", 0) == 0) {
//kill當(dāng)前進(jìn)程SystemServer
Process.killProcess(Process.myPid());
System.exit(10);
}
}
waitedHalf = false;
}
}

在此run函數(shù)中循環(huán)發(fā)送消息,判斷標(biāo)志是否正常,決定檢測對象是否正常工作。
若監(jiān)測對象不正常工作,則收集重要的stack信息保存下來,然后重啟SystemServer。
監(jiān)測消息的處理:
是在HeartbeatHandler中進(jìn)行,看看消息處理函數(shù)。
復(fù)制代碼 代碼如下:

public void handleMessage(Message msg) {
switch (msg.what) {
case MONITOR: {
// See if we should force a reboot.
//監(jiān)測對象是否正常工作中……
final int size = mMonitors.size();
for (int i = 0 ; i < size ; i++) {
//調(diào)用監(jiān)測對象的monitor接口
mCurrentMonitor = mMonitors.get(i);
mCurrentMonitor.monitor();
}
//走到這里表明監(jiān)測對象正常
synchronized (Watchdog.this) {
mCompleted = true;
mCurrentMonitor = null;
}
} break;
}
}

判斷監(jiān)測對象是否正常工作,通過調(diào)用監(jiān)測對象實現(xiàn)的接口monitor,看看這個接口該如何執(zhí)行的。
PowerManagerService中:
public void monitor() {
//判斷Service是否發(fā)生死鎖,如果發(fā)生死鎖,程序?qū)⒃诖艘恢钡却?/主要是線程間同步問題 造成死鎖
synchronized (mLocks) { }
}
以上便是Watchdog監(jiān)測Service是否正常工作的流程;我們也可以使用Watchdog來監(jiān)測別的資源如內(nèi)存等使用情況。
這個Watchdog給我們提供了一種思路,一種框架,對程序正常運(yùn)行或者資源的正常使用情況等的一種監(jiān)測機(jī)制。

相關(guān)文章

  • Android Service自啟動注意事項分析

    Android Service自啟動注意事項分析

    這篇文章主要介紹了Android Service自啟動注意事項,結(jié)合實例分析了Android Service自啟動過程中屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • Android adb logcat 命令查看日志詳細(xì)介紹

    Android adb logcat 命令查看日志詳細(xì)介紹

    這篇文章主要介紹了Android adb logcat 命令詳細(xì)介紹的相關(guān)資料,這里對logcat 命令進(jìn)行了詳細(xì)介紹,并介紹了過濾日志輸出的知識,需要的朋友可以參考下
    2016-12-12
  • Android 通過騰訊TBS實現(xiàn)文件預(yù)覽功能

    Android 通過騰訊TBS實現(xiàn)文件預(yù)覽功能

    這篇文章主要介紹了Android 通過騰訊TBS實現(xiàn)文件預(yù)覽功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Flutter Widget開發(fā)Shortcuts快捷鍵實例

    Flutter Widget開發(fā)Shortcuts快捷鍵實例

    這篇文章主要為大家介紹了Flutter Widget開發(fā)Shortcuts快捷鍵實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • android第三方分享方式的簡單實現(xiàn)

    android第三方分享方式的簡單實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了android第三方分享方式的簡單實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • android?studio數(shù)據(jù)存儲建立SQLite數(shù)據(jù)庫實現(xiàn)增刪查改

    android?studio數(shù)據(jù)存儲建立SQLite數(shù)據(jù)庫實現(xiàn)增刪查改

    這篇文章主要介紹了vandroid?studio數(shù)據(jù)存儲建立SQLite數(shù)據(jù)庫實現(xiàn)增刪查改,分別使用sqlite3工具和Android代碼的方式建立SQLite數(shù)據(jù)庫,具體內(nèi)容,需要的小伙伴可以參考下面文章得詳細(xì)內(nèi)容
    2021-12-12
  • 4種Android屏幕自適應(yīng)解決方案

    4種Android屏幕自適應(yīng)解決方案

    在開發(fā)android項目的時候,我們常常會考慮這樣的問題:為不同的手機(jī)屏幕顯示出最佳的界面,以提升用戶的體驗。本文介紹的是4種Android屏幕自適應(yīng)解決方案,有興趣的可以了解一下。
    2016-10-10
  • Android開發(fā)使用Handler的PostDelayed方法實現(xiàn)圖片輪播功能

    Android開發(fā)使用Handler的PostDelayed方法實現(xiàn)圖片輪播功能

    這篇文章主要介紹了Android開發(fā)使用Handler的PostDelayed方法實現(xiàn)圖片輪播功能,結(jié)合實例形式分析了Android基于Handler的PostDelayed方法實現(xiàn)圖片輪播功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • Android架構(gòu)組件Room的使用詳解

    Android架構(gòu)組件Room的使用詳解

    Room其實就是一個orm,抽象了SQLite的使用。這篇文章給大家介紹了Android架構(gòu)組件Room的使用詳解,需要的朋友參考下吧
    2017-12-12
  • Andriod Studio實現(xiàn)保存QQ密碼功能(案例代碼詳解)

    Andriod Studio實現(xiàn)保存QQ密碼功能(案例代碼詳解)

    這篇文章主要介紹了Andriod Studio實現(xiàn)保存QQ密碼功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論