当前位置:网站首页>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-------RTC实时时钟
- Give you a project, how will you carry out performance testing (I)
- [opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering
- What pitfalls should be avoided in the job interview for the operation post in 2022?
- EditText监听焦点
- Sword finger offer 12 Path in matrix
- Is it safe and reliable to open a securities account in Yixue school?
- LinkedIn DataHub --- 经验分享
- Is it reliable and safe to avoid five in case of stock trading account opening
- Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
猜你喜欢
随机推荐
This thing is called a jump watch?
LeetCode每日一题:实现strStr()
Stm32f407----- register address name mapping analysis
The secondary market is full of bad news. How should the market go next? One article will show you the general trend
剑指 Offer 12. 矩阵中的路径
websocket-js连接如何携带token验证
Yyds dry goods inventory building knowledge map from scratch with neo4j (I)
TypeScript -- 第二节:变量声明
Typescript -- Section 2: variable declaration
[SSM] an error is reported that the user name of the access denied for user 'WYF' @ 'localhost' (using password: yes) data becomes the user name of the computer
What will be done after digital IC Verification?
入行数字IC验证后会做些什么?
随笔记:插入排序 --from wcc
Auto encoder
MySQL connection query is easy to understand
三个pwn题
【LeetCode】21. 合并两个有序链表 - Go 语言题解
Stm32f407 ------- IO pin multiplexing mapping
Stm32f407-------- NVIC interrupt priority management
每日一题:消失的数字








