当前位置:网站首页>Basic use and internal implementation principle of handerthread
Basic use and internal implementation principle of handerthread
2022-04-22 14:22:00 【Cow in the yard】
author : Chang'an converted to his hometown
This article is mainly about HanderThread Basic use and internal implementation principle , Readers need to be right about Android The message mechanism has a certain knowledge base .
HanderThread Basic use
fun hdUse() {
val handlerThread = HandlerThread("ChapterActivity", 100)
handlerThread.start()
val handler = Handler(handlerThread.looper) {
msg ->
if (msg.what == 5) {
// Execute code logic
}
true
}
handler.sendEmptyMessage(5)
}
- First create
HandlerThread, The first parameter of the constructor represents the thread name , The second parameter represents the priority of the thread , Principle analysis later - call
HandlerThreadOfstartMethod , Must be ingetLooperCall top note before method invocation - establish
Handler, And pass inHandlerThreadOf looper - Then you can create it through the previous step
HandlerSend and processMessage了
HanderThread Collocation process
kotlinx-coroutines-android-1.4.1 Dependency provides an extension method Handler.asCoroutineDispatcher, Create a collaboration context element ——CoroutineDispatcher.
val dispatch = handler.asCoroutineDispatcher()
lifecycleScope.launch(dispatch) {
}
Finally, the logic in the coroutine block will be packaged into a Message, Add to handler Held looper In the message queue of , Final handler Held looper In which thread was created , In which thread will the coroutine block execute .

#HandlerContext

HanderThread Principle analysis
HandlerThread Inherited from Thread, It means that it creates a thread object . Here you can understand why HanderThread Why did the constructor of pass in the thread name and thread priority .
public class HandlerThread extends Thread
Then , Then call HandlerThread Of start Method , In other words, thread execution is enabled , Will execute the thread run Method :

- call
Looper.prepareMethod to create a file unique to the threadLooper - Set the priority of the current thread
- call
Looper.loop()Turn onforloop , Keep getting from the message queueMessageAnd execute on the current thread
So , Through the top HandlerThread Created handler Sent message The final execution is HandlerThread In this created thread .
HanderThread Case on
Everybody knows ,Service Time consuming operations cannot be performed in , Or you'll be buried bomb Direct explosion ANR. So the official provided a IntentService, To realize the present Service Time consuming operations in .
public abstract class IntentService extends Service
The core code is onCreate in : #IntentService

It looks like I'm familiar with , In essence, it is the use of HandlerThread Realized . however , Now this IntentService Has been marked as obsolete , The system recommends that we use :

版权声明
本文为[Cow in the yard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221416319164.html
边栏推荐
- Getting started with shell
- Lors de l'obtention d'une valeur dans la base de données, la base de données a une valeur, mais elle est vide.
- Daily question - Three Kingdoms game (diagonal matrix)
- 代码覆盖率之 sonar
- Where do embedded software bugs come from and how do they go
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
- 动规初解||最大子序和 最长上升子序列53、128
- Openmldb pulsar connector: efficiently connect real-time data to feature Engineering
- Binary Tree递归||二叉树展开为链表 530. 二叉搜索树的最小绝对差
- 做好安全测试的方法
猜你喜欢

CRM系统改善客户体验的方法

深入理解读写锁ReentrantReadWriteLock和并发容器CopyOnWriteArrayList

定时器--

Where do embedded software bugs come from and how do they go

系列解读 SMC-R (二):融合 TCP 与 RDMA 的 SMC-R 通信 | 龙蜥技术

leetcode:215. 数组中的第K个最大元素

In depth analysis of volatile principle
2020火爆全网系列:这是一份非常适合领取与收藏的Android进阶-面试重难点资料笔记!持续更新大厂高质量面试链接

HanderThread基本使用以及内部实现原理

spark代码 spark-submit提交yarn-cluster模式
随机推荐
CRM系统改善客户体验的方法
天梯赛--L2-004 这是二叉搜索树吗? (25 分)(递归)
Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
获取数组中每一项对象中的某个值的和(reduce的使用)
Solve command line is too long Shorten command line for........ error
Greedy dynamic rules 𞓜 Jumping Games 45, 55
pip命令和在线、离线安装方法
Redis相比memcached
Binarytree practice binary tree sequence traversal 𞓜 sequence traversal with queue
C语言的三子棋,用22天总结了一份完美的SQL学习笔记
26岁,0基础学软件测试还来得及吗
Leetcode (04)
Daily question - Three Kingdoms game (diagonal matrix)
Deep understanding of read-write lock reentrantreadwritelock and concurrent container copyonwritearraylist
Redis 批量删除数据(通配符)
HashTable哈希表与统计594、350、|554、609、454、18
Hashtable hash table practice finding, inserting, deleting 217, 349, 202, 287, 290, 532, 205, 128
代码覆盖率之 sonar
回溯 全排列 第k个排列 优美的排列 组合 组合总和 N皇后I II
多线程初阶