源码分析/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.os;/*** Handy class for starting a new thread that has a looper. The looper can then be* used to create handler classes. Note that start() must still be called.*/public class HandlerThread extends Thread {int mPriority; int mTid = -1;Looper mLooper; public HandlerThread(String name) {super(name);mPriority = Process.THREAD_PRIORITY_DEFAULT;}/** * Constructs a HandlerThread. * @param name* @param priority The priority to run the thread at. The value supplied must be from * {@link android.os.Process} and not from java.lang.Thread. */public HandlerThread(String name, int priority) {super(name);mPriority = priority;}/** * Call back method that can be explicitly overridden if needed to execute some * setup before Looper loops. */protected void onLooperPrepared() {}@Overridepublic void run() {mTid = Process.myTid();Looper.prepare(); synchronized (this) {mLooper = Looper.myLooper();notifyAll();}Process.setThreadPriority(mPriority);onLooperPrepared();Looper.loop();mTid = -1;}/** * This method returns the Looper associated with this thread. If this thread not been started * or for any reason is isAlive() returns false, this method will return null. If this thread * has been started, this method will block until the looper has been initialized. * @return The looper. */public Looper getLooper() {if (!isAlive()) {return null;}// If the thread has been started, wait until the looper has been created.synchronized (this) {while (isAlive() && mLooper == null) {try {wait();} catch (InterruptedException e) {}}}return mLooper;}/** * Quits the handler thread's looper. *

* Causes the handler thread's looper to terminate without processing any * more messages in the message queue. *

* Any attempt to post messages to the queue after the looper is asked to quit will fail. * For example, the {@link Handler#sendMessage(Message)} method will return false. *

* Using this method may be unsafe because some messages may not be delivered * before the looper terminates. Consider using {@link #quitSafely} instead to ensure * that all pending work is completed in an orderly manner. *

* * @return True if the looper looper has been asked to quit or false if the * thread had not yet started running. * * @see #quitSafely */public boolean quit() {Looper looper = getLooper(); if (looper != null) {looper.quit(); return true;}return false;}/** * Quits the handler thread's looper safely. *

* Causes the handler thread's looper to terminate as soon as all remaining messages * in the message queue that are already due to be delivered have been handled. * Pending delayed messages with due times in the future will not be delivered. *

* Any attempt to post messages to the queue after the looper is asked to quit will fail. * For example, the {@link Handler#sendMessage(Message)} method will return false. *

* If the thread has not been started or has finished (that is if * {@link #getLooper} returns null), then false is returned. * Otherwise the looper is asked to quit and true is returned. *

* * @return True if the looper looper has been asked to quit or false if the * thread had not yet started running. */public boolean quitSafely() {Looper looper = getLooper(); if (looper != null) {looper.quitSafely(); return true;}return false;}/** * Returns the identifier of this thread. See Process.myTid(). */public int getThreadId() {return mTid;}}

java handlerthread_Android中HandlerThread详解相关推荐

  1. java switch中if_详解java中if语句和switch的使用

    if语句 说起if语句,那么不得不提起三元运算符 a>b?c=0:c=1; 如果a>b结果为true,那么c=0,为false,c=1 那么下面看下if语句 if(a>b)c=0; ...

  2. java arrays_Java中Arrays详解

    一.Arrays类的定义 Arrays类位于 java.util包中,主要包含了操纵数组的各种方法 使用时导包:import java.util.Arrays 二.Arrays常用函数(都是静态的) ...

  3. java socket中属性详解_前端开发:关于Vue组件中的data属性值是函数而不是对象的详解...

    最近在搞关于前端开发的基础知识归纳,发现了不少经典知识点,那么本篇博文就来分享一个经典的知识点:Vue组件中的data属性值为什么是函数而不是对象.首先来了解一下Vue组件的使用理念:在Vue组件使用 ...

  4. java socket中属性详解_新款宝马5系详解,豪华中大型轿车也能主打运动、科技属性...

    在2020年北京车展上,宝马推出了5系的中期改款版本,共17款车型.其中,长轴距燃油版有10款车型,售价区间为42.69万-54.99万元:标准轴距燃油版有4款车型,售价区间为42.39万-60.39 ...

  5. Java中CAS详解

    转载自  Java中CAS详解 在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换 ...

  6. Java中LinkedList详解

    Java中LinkedList详解 LinkedList底层是双向链表 单向链表 双向链表 LinkedList新增的方法 主要增加了针对头结点与尾结点进行操作的方法, 即针对第一个元素和最后一个元素 ...

  7. Java中super详解

    目录 Java中super详解 super的作用: 1.     通过super可以访问父类的构造方法 2.   通过super可以访问父类的属性(非私有) 3.        通过super可以访问 ...

  8. Java单元测试之JUnit4详解

    2019独角兽企业重金招聘Python工程师标准>>> Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @B ...

  9. java -jar 和 -cp详解

    java -jar 和 -cp详解 命令行执行程序 假如我们有一个程序,把它打包成Test.jar,如何运行才能成功输出Hello World package com.test; public cla ...

最新文章

  1. 无人驾驶汽车开发平台,加速无人驾驶汽车的商业化
  2. 【强化学习篇】--强化学习从初识到应用
  3. 小豆包的学习之旅:里程计运动模型
  4. 洛谷 - P2756 飞行员配对方案问题(二分图最大匹配+路径打印)
  5. android中一些常用的VIEW动作类型
  6. [重要] Cocos2dx 3.0 PageView ListView 在Android设备下背景显示为绿色的问题的解决方案
  7. python斐波那契数列_python3实现斐波那契数列(4种方法)
  8. 一分钟了解TCP/IP网络通信协议
  9. JSP基础教程-初时index首页
  10. Linux系统怎么复制文件夹下的全部文件到另外文件夹?...
  11. python if并列条件_Python中if有多个条件怎么办
  12. 人工神经网络心得体会_图神经网络打卡心得体会
  13. 自然语言处理之语言模型与词向量
  14. 高德地图:弧度飞线图层详解
  15. 有道云笔记设置界面 占满全屏(右侧)
  16. 化工厂人员定位如何实现,可以解决哪些问题?
  17. 天载优配解读全商场亏钱效应浓郁
  18. 半导体精密划片机行业介绍及市场分析
  19. 如何安装 Composer
  20. 医院需要什么样的集成平台

热门文章

  1. c#利用TCP/IP协议与川崎机器人进行通信
  2. 关于SD卡随着写入文件的增多时间变慢原因以及解决办法
  3. Windows7服务一览
  4. php代理(proxy)模式
  5. HDMI、DVI、VGA、RGB、分量、S端子)高清接口图片说明
  6. 视频接口大全(HDMI、DVI、VGA、RGB、分量、S端子、USB接口)
  7. 人手一个数字人还有多远?百度吴甜做客央视《对话》
  8. 当程序员真的好累——IT界那些笑话
  9. noip2012提高组模拟题樱桃庄园
  10. 实时系统vxWorks - 虚拟机环境搭建