当前位置:网站首页>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
}
边栏推荐
- Redis: simple use of redis
- File class (check)
- Groovy, "try with resources" construction alternative
- easyExcel
- iptables 4层转发
- 【Camera专题】手把手撸一份驱动 到 点亮Camera
- When the epidemic comes, how to manage the team as a leader| Community essay solicitation
- [fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
- Problems encountered in small program development of dark horse shopping mall
- Machine learning notes (constantly updating...)
猜你喜欢

¢ growth path and experience sharing of getting an offer

How to deal with cache hot key in redis
![[camera topic] complete analysis of camera dtsi](/img/cb/d42589fcf0610600c9dc8c7992d4d7.png)
[camera topic] complete analysis of camera dtsi

创建+注册 子应用_定义路由,全局路由与子路由
![[camera topic] how to save OTP data in user-defined nodes](/img/3e/b76c4d6ef9ab5f5b4326a3a8aa1c4f.png)
[camera topic] how to save OTP data in user-defined nodes

Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
![[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)

easyPOI

MySQL learning 03

Niuniu's ball guessing game (dynamic planning + prefix influence)
随机推荐
Network security - password cracking
Rockchip3399 start auto load driver
His experience in choosing a startup company or a big Internet company may give you some inspiration
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
Analysis, use and extension of open source API gateway apisex
When the epidemic comes, how to manage the team as a leader| Community essay solicitation
Depth (penetration) selector:: v-deep/deep/ and > > >
stm32F407-------ADC
How do browsers render pages?
How to deal with cache hot key in redis
Basic operation of view
Explore the conversion between PX pixels and Pt pounds, mm and MM
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
Learn BeanShell before you dare to say you know JMeter
[camera topic] complete analysis of camera dtsi
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
[shutter] pull the navigation bar sideways (drawer component | pageview component)
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
Modify table structure