当前位置:网站首页>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
边栏推荐
- What is the lifecycle of automated testing?
- stm32F407-------IO引脚复用映射
- TypeScript -- 第二节:变量声明
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
- 在线买股票开户安全嘛?
- IO playback function of FIO
- [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
- ctfshow XSS
- Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
- ctfshow XSS
猜你喜欢

剑指 Offer 12. 矩阵中的路径

Learning fuzzy from SQL injection to bypass the latest safe dog WAF

TypeScript -- 第一节:基础类型

What are some tips to improve your interview success rate?

Auto encoder
![SQL note 2 [MySQL]](/img/a4/f711173ce731d95860746e309b7d74.jpg)
SQL note 2 [MySQL]

这玩意叫跳表?

Behaviortree in ros2

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

Yyds dry goods inventory building knowledge map from scratch with neo4j (I)
随机推荐
mysql 高可用双主同步
Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
Quartz explanation and use
What will be done after digital IC Verification?
How to solve the database type error in the operation of the servert project?
PHP function file_ get_ Contents and memory mapping of operating system
Stm32f407 ------- IO pin multiplexing mapping
stm32F407-------NVIC中断优先级管理
三個pwn題
EditText监听焦点
Typescript-- section 4: Functions
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
Three PWN questions
Test experience: how testers evolve from 0 to 1
Stm32f407 ------ serial (serial port) communication
The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been
[software analysis] iterative explanation of software analysis, design and modeling
Behaviortree in ros2
一条update语句到底加了多少锁?带你深入理解底层原理
随笔记:模拟类数组(array-like)的方法