当前位置:网站首页>HandlerThread使用及原理
HandlerThread使用及原理
2022-06-29 00:00:00 【LLAiden】
首先看一下继承接口

从上面截图中可以看到他是个线程类,既然是线程类那么先看到run方法
@Override
public void run() {
mTid = Process.myTid();
//创建当前线程的Looper对象
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
//启动轮询
Looper.loop();
mTid = -1;
}在run方法中主要就是创建Looper和启动looper的消息分发,看到了Looper那自然避不开Handler
/**
* @return a shared {@link Handler} associated with this thread
* @hide
*/
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}在HandlerThread中就有一个获取当前Handler的方法,这个方法被带上的hide注解我们在开发中无法自己调用需要新建handler传入Looper如下代码
val handlerThread = HandlerThread("handleThread")
handlerThread.start()
val looper = handlerThread.looper
Handler(looper).post {
Log.e(tag, "onCreate: ${Thread.currentThread().name}")
}用这个Handler发送的消息就会在这个新建的线程中执行,这里有个则是通过HandlerThread#getLooper之前需要先调用线程的Thread#start让其先执行run方法不然会阻塞进入wait状态
public Looper getLooper() {
if (!isAlive()) {
return null;
}
boolean wasInterrupted = false;
// If the thread has been started, wait until the looper has been created.
synchronized (this) {
//mLooper会在run方法中被创建
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
wasInterrupted = true;
}
}
}
/*
* We may need to restore the thread's interrupted flag, because it may
* have been cleared above since we eat InterruptedExceptions
*/
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
return mLooper;
}结合上面看到的源码总结出HandlerThread就是Thread+Handler是个适合执行耗时任务的Handler
边栏推荐
- 请问指南针股票软件可靠吗?在上面交易股票安全吗?
- stm32F407-------时钟系统(SystemInit时钟初始化、Systick滴答定时器)
- Is it safe and reliable to open a securities account in Yixue school?
- What are the virtual machine software? What are their respective roles?
- 在线买股票开户安全嘛?
- Stm32f407 ------- IO pin multiplexing mapping
- IO playback function of FIO
- Typescript -- Section 6 generic
- 6.28 learning content
- Is the compass stock software reliable? Is it safe to trade stocks on it?
猜你喜欢

Technology sharing | software development process that you must understand if you want to get started with testing

每日一题: 数组中数字出现的次数

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

每日一题:数组中数字出现的次数2

Online yaml to JSON tool

Typescript -- Section 3: Interface

转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)

stm32F407-------串行(串口)通信

The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been

Behaviortree in ros2
随机推荐
每日一题:移除元素
Leetcode 178 Score ranking (June 27, 2022)
Is it safe and reliable to open a securities account in Yixue school?
Notes: three ways to define setters and Getters
Along with the notes: methods simulating array like classes
12. object detection mask RCNN
炒股开户万一免五是靠谱么,安全么
oracle 去掉html标签
随笔记:模拟类数组(array-like)的方法
What are some tips to improve your interview success rate?
小白创业做电商,选对商城系统很重要!
stm32F407-------NVIC中断优先级管理
Implementation of dynamic timer for quartz
stm32F407-------GPIO输入实验
从SQL注入绕过最新安全狗WAF中学习fuzz
Typescript -- Section 1: basic types
Stm32f407 ------- RTC real time clock
12.物體檢測Mask-Rcnn
Haskell 配置 VS code 开发环境 (2022年6月)
点击劫持:X-Frame-Options未配置