当前位置:网站首页>Kotlin middle process understanding and Practice (II)
Kotlin middle process understanding and Practice (II)
2022-07-03 02:07:00 【yu-Knight】
Kotlin Understanding and practice of Zhongxie Cheng ( Two )
Coroutine builder
One .launch And async Builders are used to start new processes
1.launch, Return to one Job And without any result value .
2.async, Return to one Deferred, Deferred Also a Job, have access to .await() Get its final result on a delayed value .
Two , Waiting for a job
1.join And await
2. Combined concurrency
/** Use runBlocking Wrap the main thread as a coroutine Top collaboration GlobalScope, The life cycle is process Grade , Even if Activity perhaps Fragment Has been destroyed , The collaboration process is still being implemented . */
// join And await
@Test
fun `test coroutine builder`() = runBlocking {
//job1 Zixie Cheng
val job1 = launch {
delay(200)
println("job1 finished.")
}
//job2 Zixie Cheng
val job2 = async {
delay(200)
println("job2 finished.")
"job2 result" //async Return value
}
println(job2.await())
/** job1 finished. job2 finished. job2 result */
}
@Test
fun `test coroutine join`() = runBlocking {
//job1 Zixie Cheng
val job1 = launch {
delay(2000)
println("One")
}
job1.join() // Execute first job1 coroutines , Re execution job2,job3 coroutines
//job2 Zixie Cheng
val job2 = launch {
delay(200)
println("Two")
}
//job3 Zixie Cheng
val job3 = launch {
delay(200)
println("Three")
}
/** One Two Three */
}
@Test
fun `test coroutine await`() = runBlocking {
//job1 Zixie Cheng
val job1 = async {
delay(2000)
println("One")
}
job1.await() // Execute first job1 coroutines , Re execution job2,job3 coroutines
//job2 Zixie Cheng
val job2 = async {
delay(200)
println("Two")
}
//job3 Zixie Cheng
val job3 = async {
delay(200)
println("Three")
}
/** One Two Three */
}
// Combined concurrency
@Test
fun `test sync`() = runBlocking {
val time = measureTimeMillis {
val one = doOne()
val two = doTwo()
println("The result:${
one + two}")
}
println("Completed in $time ms")
/** The result:26 Completed in 2020 ms */
}
@Test
fun `test combine async`() = runBlocking {
val time = measureTimeMillis {
val one = async {
doOne() }
val two = async {
doTwo() }
println("The result:${
one.await() + two.await()}")
// val one = async { doOne() }.await()
// val two = async { doTwo() }.await()
// println("The result:${one + two}") //Completed in 2020 ms
}
println("Completed in $time ms")
/** The result:26 Completed in 1020 ms */
}
private suspend fun doOne(): Int {
delay(1000)
return 14
}
private suspend fun doTwo(): Int {
delay(1000)
return 12
}
边栏推荐
- The testing process that software testers should know
- Network security - man in the middle attack
- Basic operation of view
- es6 filter() 数组过滤方法总结
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- How do browsers render pages?
- Network security - phishing
- How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
- Stm32f407 ------- IIC communication protocol
- 【Camera专题】OTP数据如何保存在自定义节点中
猜你喜欢
![[leetcode] 797 and 1189 (basis of graph theory)](/img/2a/9c0a904151a17c2d23dea9ad04dbfe.jpg)
[leetcode] 797 and 1189 (basis of graph theory)

y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
![[camera topic] complete analysis of camera dtsi](/img/cb/d42589fcf0610600c9dc8c7992d4d7.png)
[camera topic] complete analysis of camera dtsi

Anna: Beibei, can you draw?

Everything file search tool

机器学习笔记(持续更新中。。。)
![[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)](/img/e7/915404743d6639ac359bb4e7f7fbb7.jpg)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)

Analysis, use and extension of open source API gateway apisex

How do it students find short-term internships? Which is better, short-term internship or long-term internship?

Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
随机推荐
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
Niuniu's ball guessing game (dynamic planning + prefix influence)
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
Modify table structure
easyExcel
Everything file search tool
【Camera专题】手把手撸一份驱动 到 点亮Camera
Machine learning notes (constantly updating...)
Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
[Yu Yue education] reference materials of love psychology of China University of mining and technology
Network security - talking about security threats
[shutter] pull the navigation bar sideways (drawer component | pageview component)
How to refresh the opening amount of Oracle ERP
The technology boss is ready, and the topic of position C is up to you
Where is the future of test engineers? Confused to see
全链路数字化转型下,零售企业如何打开第二增长曲线
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
Network security ACL access control list
How do it students find short-term internships? Which is better, short-term internship or long-term internship?