当前位置:网站首页>Kotlin - Coroutine Dispatcher CoroutineDispatcher
Kotlin - Coroutine Dispatcher CoroutineDispatcher
2022-08-06 20:13:00 【lazy programmer】
- is an abstract class, and Dispatcher is a helper class in the standard library that helps us encapsulate switching threads, and can schedule coroutines on which type of threads to execute.
- Because the child coroutine will inherit the context of the parent coroutine, after specifying the scheduler mode on the parent coroutine, the child coroutine uses this mode by default.
- IO and DEFAULT modes share the same thread pool, optimized threads are reused, and the number of threads is limited independently and will not starve each other.If used together to the maximum extent, the default number of active threads at the same time is 64+CPU number.
| Dispatcher.Main | Runs on the main thread, which is the UI thread in Android, and is used to handle some lightweight tasks of UI interaction. | Call suspend function Call UI function Update LiveData |
| Dispatcher.Main.immediate | Coroutine scheduling has a cost. When we are already in the default main thread of the APP, opening a sub-coroutine (a suspend function dispatched to the main thread) is aSmall but unnecessary overhead (e.g. ViewModelScope is on Android's default main thread, so the context's scheduler uses this).In this case, specifying immediate will only be scheduled when necessary.Currently other schedulers do not support this mode. | The function is wrapped withContext to be used when running on Dispatcher.Main. |
| Dispatcher.IO | Runs on a thread pool, optimized for blocking tasks. Default limit of concurrently active threads ≤ 64 | Database File Read/Write Network Processing |
| Dispatcher.Default | Runs on a thread pool optimized for CPU-intensive computing tasks. 2 ≤ number of threads ≤ number of CPUs | array sorting Json parsing processing difference judgment |
| Dispatcher.Unconfined | Does not change the thread, executes on the thread that started it, and resumes its execution on the thread. | Use when you don't need to care which thread the coroutine is suspended on. |
Kotlin version 1.6
limitedParallelism() | public open fun limitedParallelism(parallelism: Int): CoroutineDispatcher |
| Default mode: When there is a task with a lot of overhead, other coroutines using the same scheduler may not be able to grab the thread execution right. At this time, it can be used to limit the coroutineThe number of threads used by the process. | |
| IO mode: When there is a task with a lot of overhead, it may cause too many threads to be blocked and other tasks to suspend and wait, breaking the default limit of 64 threads to speed up execution. | |
| The limit is 1 to solve the synchronization problem of multi-threaded data modification. |
suspend fun main(): Unit = coroutineScope {//Use the default IO modelaunch {printTime(Dispatchers.IO) //Print: Dispatchers.IO cost: 2038/}//Add threads using limitedParallelismlaunch {val dispatcher = Dispatchers.IO.limitedParallelism(100)printTime(dispatcher) / print: [email protected] Cost: 1037}}suspend fun printTime(dispatcher: CoroutineDispatcher) {val time = measureTimeMillis {coroutineScope {repeat(100) {launch(dispatcher) {Thread.sleep(1000)}}}}println("$dispatcher spent: $time")}边栏推荐
猜你喜欢

为什么Video Speed Manager 和 Video Speed Controller 的chrome插件对有些B站视频不能调速

在ArcGIS Earth中自动化的绘制复杂图形

如何给WordPress博客网站换个漂亮的字体

A question that most people get wrong: If I want to store IP addresses, what data type is better?

svn项目的拉取和提交

61:第五章:开发admin管理服务:14:开发【友情链接列表查询,接口】;(核心是:理解MongoDB,查询数据的逻辑)

pycharm 关闭光标闪烁

如何借助cpolar内网穿透连接本地树莓派(1)

Eye Tracking for Everyone 译文版

Ali's second side: How to perform performance tuning with sudden increase in interface traffic?
随机推荐
MySQL中索引的基本知识
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) F
rce code and command execution vulnerability and file inclusion
R语言ggplot2可视化:基于aes函数中的fill参数自定义绘制分组折线图并添加数据点(散点)、使用scale_fill_manual函数手动添加数据点颜色度量向量(使用十六进制颜色)
JDY-16 蓝牙4.2模块串口测试方法
WebRTC-NACK、Pacer和拥塞控制和FEC
How to Give Your WordPress Blog Site a Beautiful Font
我们来聊聊锁升级吧
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声、使用dpill函数筛选合适的带宽值(bandwidth)
如何自动识别爬虫网页的编码
LaunchScreen.storyboard 颜色设置
CI/CD持续集成/持续部署
如何运营外贸独立站
pycharm 关闭光标闪烁
Unity API详解——GameObject类
go server和client通过grpc建立连接
Pytest Learn - YAML
Locust 之 TaskSet类梳理
多线程-并发问题
MySQL中存储引擎之间的对比