国产探花免费观看_亚洲丰满少妇自慰呻吟_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); } } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五指山市| 吉水县| 海兴县| 永登县| 浦东新区| 海丰县| 老河口市| 迁安市| 滨海县| 莱州市| 连城县| 固阳县| 玛曲县| 洞口县| 潜山县| 宜兰县| 龙山县| 紫阳县| 浮梁县| 公安县| 安徽省| 基隆市| 大竹县| 禄劝| 什邡市| 东明县| 临澧县| 通海县| 龙里县| 邵武市| 台东县| 新平| 邓州市| 平陆县| 亚东县| 淳安县| 瓦房店市| 蒙阴县| 衡山县| 正镶白旗| 新宾|