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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

HandlerThread源碼分析

2019-11-09 19:05:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

HandlerThread源碼分析

public class HandlerThread extends Thread { //線程優(yōu)先級(jí) 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; } //可以覆寫,在循環(huán)之前做一些處理 protected void onLooperPrepared() { } //創(chuàng)建了looper并通知getLooper。在執(zhí)行g(shù)etLooper之前必須先執(zhí)行thread的start方法,不然getLooper將返回null,容易引發(fā)空指針 @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停止接收新的任務(wù)并清空MessageQueue中的任務(wù) public boolean quit() { Looper looper = getLooper(); if (looper != null) { looper.quit(); return true; } return false; } //looper停止接收新的任務(wù)并清空MessageQueue中的延時(shí)任務(wù),非延時(shí)任務(wù)將繼續(xù)執(zhí)行 public boolean quitSafely() { Looper looper = getLooper(); if (looper != null) { looper.quitSafely(); return true; } return false; } public int getThreadId() { return mTid; }}

Looper#quit相關(guān)源碼

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); } } }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 景洪市| 长兴县| 贵港市| 甘谷县| 全南县| 安顺市| 博白县| 林口县| 霍林郭勒市| 调兵山市| 吕梁市| 武定县| 略阳县| 光山县| 子洲县| 桐庐县| 绥宁县| 美姑县| 乌审旗| 鄂温| 克拉玛依市| 潜山县| 两当县| 府谷县| 孟津县| 高邮市| 安宁市| 渝北区| 西华县| 抚宁县| 万宁市| 玛纳斯县| 六枝特区| 嫩江县| 璧山县| 昂仁县| 都兰县| 宕昌县| 龙口市| 三明市| 布尔津县|