当前位置:网站首页>Coroutinecontext in kotlin
Coroutinecontext in kotlin
2022-07-03 02:07:00 【yu-Knight】
List of articles
The context of the coroutine
CoroutineContext Is a set of elements that define the behavior of a coroutine . It consists of the following :
- 1.Job: Control the lifecycle of the process .
- 2.CoroutineDispatcher: Distribute tasks to the appropriate threads .
- 3.CoroutineName: The name of the program , It's useful when debugging .
- 4.CoroutineExceptionHandle: Handling uncapped exceptions .
Combine elements in context
Sometimes we need to define multiple elements in the context of a collaboration . We can use + Operator to implement . for instance , We can specify a scheduler to start the process and specify a name at the same time :
@Test
fun `test CoroutineContext`() = runBlocking<Unit> {
launch(Dispatchers.Default + CoroutineName("test")) {
println("I'm working in thread${
Thread.currentThread().name}")
}
}
Inheritance of coroutine context
- For the newly created collaboration , its CoroutineContext Will include a new Job example , It will help us to control the lifecycle of the coprocessor . And the rest of the elements go from CoroutineContext Inherits from the parent class of , The parent class may be another coroutine or the one that created it CoroutineScope
@Test
fun `test CoroutineContext extend`() = runBlocking<Unit> {
val scope = CoroutineScope(Job() + Dispatchers.IO + CoroutineName("test"))
val job = scope.launch {
// The new program will CoroutineScope As a parent
println("${
coroutineContext[Job]} ${
Thread.currentThread().name}")
val result = async {
// adopt async The new coroutine created will take the current coroutine as a parent
println("${
coroutineContext[Job]} ${
Thread.currentThread().name}")
"OK"
}.await()
}
job.join()
/** "test#2":StandaloneCoroutine{Active}@11443e84 DefaultDispatcher-worker-1 @test#2 "test#3":DeferredCoroutine{Active}@2cf1f044 DefaultDispatcher-worker-3 @test#3 */
}
The context of the coroutine = The default value is + inherited CoroutineContext + Parameters
- Some elements contain default values :Dispatchers.Default By default CoroutineDispatcher, as well as “coroutine” By default CoroutineName;
- inherited CoroutineContext yes CoroutineScope Or his father's CoroutineContext;
- Parameters passed into the collaboration builder take precedence over inherited context parameters , Therefore, the corresponding parameter value will be overridden .
- The ultimate parent CoroutineContext It will contain Dispatchers.IO instead of scope In the object Dispatcher.Main, Because it is overwritten by the parameters in the constructor of the collaboration . Besides , Pay attention to the parent CoroutineContext Inside Job yes scope Object's Job( Red ), And new Job example ( green ) Will be assigned to a new coroutine CoroutineContext.
@Test
fun `test CoroutineContext extend2`() = runBlocking<Unit> {
val coroutineExceptionHandler = CoroutineExceptionHandler {
_, exception ->
println("Caught $exception")
}
val scope = CoroutineScope(Job() + Dispatchers.Main + coroutineExceptionHandler)
// new CoroutioneContext = Parent CoroutineContext + Job()
val job = scope.launch(Dispatchers.IO) {
// New association
}
}
边栏推荐
- [Yu Yue education] China Ocean University job search OMG reference
- Visual yolov5 format data set (labelme JSON file)
- Summary of ES6 filter() array filtering methods
- Iptables layer 4 forwarding
- easyPOI
- Network security - dynamic routing protocol rip
- Machine learning notes (constantly updating...)
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- String replace space
- 我的创作纪念日
猜你喜欢
His experience in choosing a startup company or a big Internet company may give you some inspiration
机器学习笔记(持续更新中。。。)
Niuniu's ball guessing game (dynamic planning + prefix influence)
Certaines fonctionnalités du développement d'applets
小程序开发的部分功能
深度学习笔记(持续更新中。。。)
How to deal with cache hot key in redis
Rockchip3399 start auto load driver
全链路数字化转型下,零售企业如何打开第二增长曲线
Processing of tree structure data
随机推荐
PyTorch 卷积网络正则化 DropBlock
苏世民:25条工作和生活原则
Network security - Information Collection
Processing of tree structure data
单词单词单词
Network security - the simplest virus
[Yu Yue education] China Ocean University job search OMG reference
easyPOI
函数的定义和调用、this、严格模式、高阶函数、闭包、递归
机器学习流程与方法
A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
Summary of ES6 filter() array filtering methods
Machine learning notes (constantly updating...)
【Camera专题】OTP数据如何保存在自定义节点中
Storage basic operation
Cfdiv2 Fixed Point Guessing - (2 points for Interval answer)
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
Query product cases - page rendering data
Internal connection query and external connection