国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HandlerThread源碼分析

2019-11-09 18:34:45
字體:
來源:轉載
供稿:網友

HandlerThread源碼分析

public class HandlerThread extends Thread { //線程優先級 int mPRiority; //線程id int mTid = -1; Looper mLooper; public HandlerThread(String name) { super(name); mPriority = Process.THREAD_PRIORITY_DEFAULT; } public HandlerThread(String name, int priority) { super(name); mPriority = priority; } //可以覆寫,在循環之前做一些處理 protected void onLooperPrepared() { } //創建了looper并通知getLooper。在執行getLooper之前必須先執行thread的start方法,不然getLooper將返回null,容易引發空指針 @Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; } public Looper getLooper() { if (!isAlive()) { return null; } synchronized (this) { while (isAlive() && mLooper == null) { try { wait(); } catch (InterruptedException e) { } } } return mLooper; } //looper停止接收新的任務并清空MessageQueue中的任務 public boolean quit() { Looper looper = getLooper(); if (looper != null) { looper.quit(); return true; } return false; } //looper停止接收新的任務并清空MessageQueue中的延時任務,非延時任務將繼續執行 public boolean quitSafely() { Looper looper = getLooper(); if (looper != null) { looper.quitSafely(); return true; } return false; } public int getThreadId() { return mTid; }}

Looper#quit相關源碼

public void quit() { mQueue.quit(false); }public void quitSafely() { mQueue.quit(true);}void quit(boolean safe) { if (!mQuitAllowed) { throw new IllegalStateException("Main thread not allowed to quit."); } synchronized (this) { if (mQuitting) { return; } mQuitting = true; if (safe) { removeAllFutureMessagesLocked(); } else { removeAllMessagesLocked(); } // We can assume mPtr != 0 because mQuitting was previously false. nativeWake(mPtr); } }private void removeAllMessagesLocked() { Message p = mMessages; while (p != null) { Message n = p.next; p.recycleUnchecked(); p = n; } mMessages = null; } private void removeAllFutureMessagesLocked() { final long now = SystemClock.uptimeMillis(); Message p = mMessages; if (p != null) { if (p.when > now) { removeAllMessagesLocked(); } else { Message n; for (;;) { n = p.next; if (n == null) { return; } if (n.when > now) { break; } p = n; } p.next = null; do { p = n; n = p.next; p.recycleUnchecked(); } while (n != null); } } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 斗六市| 错那县| 陈巴尔虎旗| 彩票| 常州市| 玉溪市| 彩票| 正蓝旗| 崇阳县| 霍林郭勒市| 华蓥市| 汉沽区| 来宾市| 临高县| 门源| 泸水县| 九龙县| 常熟市| 武威市| 蒙城县| 高雄市| 鱼台县| 平凉市| 措勤县| 日喀则市| 昭通市| 洱源县| 霍林郭勒市| 涿鹿县| 北川| 花莲市| 新营市| 平利县| 汕尾市| 尤溪县| 万州区| 永胜县| 万州区| 绥棱县| 金华市| 张家港市|