当前位置:网站首页>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
边栏推荐
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
- LinkedIn datahub - experience sharing
- MySQL connection query is easy to understand
- What are some tips to improve your interview success rate?
- How to make two objects or arrays equal
- Typescript -- Section 5: classes
- stm32F407-------RTC实时时钟
- What are the virtual machine software? What are their respective roles?
- Scrapy uses xlwt to implement the exporter that exports data in Excel format
- 【C Primer Plus第二章课后编程题】
猜你喜欢

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

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
![[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering](/img/1d/3a46517cbfa90005a15d7858d81ca9.png)
[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering

stm32F407-------寄存器地址名称映射分析

"Five considerations" for safe use of the Internet

stm32F407-------LCD

WPF implementation calls local camera~

好用免费的PPT模板

Stm32f407------- general timer

What are the virtual machine software? What are their respective roles?
随机推荐
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
scp拷贝文件夹
11.目标分割
Quartz explanation and use
12. Détection d'objets Mask rcnn
《英语语法新思维 基础版2》读书笔记(一)
Give you a project, how will you carry out performance testing (I)
Typescript -- Section 5: classes
Trois questions PWN
Notes: three ways to define setters and Getters
EditText监听焦点
Phoenix安装教程
stm32F407-------LCD
Summary of software testing cognition
What pitfalls should be avoided in the job interview for the operation post in 2022?
Technology sharing | software development process that you must understand if you want to get started with testing
Typescript -- Section 2: variable declaration
点击劫持:X-Frame-Options未配置
Rongyun communication solution solves the pain points of enterprise communication
WPF implementation calls local camera~