当前位置:网站首页>Use and principle of handlerthread
Use and principle of handlerthread
2022-06-29 00:05:00 【LLAiden】
First, let's look at the inheritance interface

From the screenshot above, you can see that it is a thread class , Since it is a thread class, first see run Method
@Override
public void run() {
mTid = Process.myTid();
// Create the... Of the current thread Looper object
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
// Start polling
Looper.loop();
mTid = -1;
}stay run The main method is to create Looper And start looper The distribution of messages , I saw it Looper That's natural Handler
/**
* @return a shared {@link Handler} associated with this thread
* @hide
*/
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}stay HandlerThread One of them gets the current Handler Methods , This method was brought to hide Annotation we cannot call by ourselves during development and need to create handler Pass in Looper The following code
val handlerThread = HandlerThread("handleThread")
handlerThread.start()
val looper = handlerThread.looper
Handler(looper).post {
Log.e(tag, "onCreate: ${Thread.currentThread().name}")
}Use this Handler The sent message will be executed in the new thread , Here's one through HandlerThread#getLooper You need to call the thread before Thread#start Let's do it first run Method or it will block the entry wait state
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 Will be in run Method is created
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;
}Combined with the source code seen above, it is concluded that HandlerThread Namely Thread+Handler It is suitable for performing time-consuming tasks Handler
边栏推荐
- 随笔记:重新认识 else if
- LeetCode每日一题:实现strStr()
- Is it safe to open an account for buying stocks online?
- HandlerThread使用及原理
- Sword finger offer 12 Path in matrix
- stm32F407-------LCD
- 随笔记:插入排序 --from wcc
- urllib. Parse parses the parameters in the URL connection
- 小白创业做电商,选对商城系统很重要!
- Scrapy uses xlwt to implement the exporter that exports data in Excel format
猜你喜欢
![SQL note 2 [MySQL]](/img/a4/f711173ce731d95860746e309b7d74.jpg)
SQL note 2 [MySQL]

Summary of software testing cognition

LinkedIn DataHub --- 经验分享

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)

This thing is called a jump watch?

LinkedIn datahub - experience sharing

Stm32f407------- external interrupt

11.目标分割

好用免费的PPT模板

What are the virtual machine software? What are their respective roles?
随机推荐
炒股开户万一免五是靠谱么,安全么
With notes: re understanding else if
stm32F407-------LCD
How to make two objects or arrays equal
TypeScript--第四节:函数
This thing is called a jump watch?
websocket-js连接如何携带token验证
ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql
请问指南针股票软件可靠吗?在上面交易股票安全吗?
三個pwn題
Online yaml to JSON tool
PHP function file_ get_ Contents and memory mapping of operating system
Typescript -- Section 1: basic types
TypeScript -- 第七节 枚举
Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6
Typescript -- Section 7 enumeration
好用免费的PPT模板
《英语语法新思维 基础版2》读书笔记(一)
stm32F407-------IO引脚复用映射
在线买股票开户安全嘛?