Coroutine
Phineas Huang
Why coroutine
Query Data
What is problem ?
Token
User list
{
userList: [
0001,
0002,
0003
]
}
User Info * M
{
Id: 0001,
Name: HelloWorld
Url: https://xxx.app
}
Relationship
User Info * N
CRUD (
Creat,
Read,
Update,
Delete
)
???
Closure Hell
Solutions
Backend-Side Frontend-Side
GraphQL Waterfall
Other ?
What is Coroutine
https://tw.saowen.com/a/
115ef92ba67ee6a64a7be95052653ec2b441e82a7fe628af8df3b62e878fcdbf
Code
Result
Running
How to use
Case I
Define context
Simulate calculate time
Log
Case I
Case I
Main function
Run in background
thread
Case II
Case II
Case II
Main function
Package
Case III
Case III
Case III
Main function
Case III
Conclusion
Conclusion
• 歷歷史上是先有 Coroutine,⽤用來來模擬 OS 多項 task 同時執
⾏行行

• Thread 效能上還是⽐比 Coroutine 好,但時常會因為 lock 設
計不不良,導致許多問題衍⽣生。

• Coroutine 僅在 I/O 處理理上,效能可贏過 Thread

• Coroutine 也是需要 lock,並不不保證 Thread safe
Risk
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/index.html
Github
• https://github.com/SunXiaoShan/DemoCoroutine
Reference
• https://www.zhihu.com/question/20511233

• https://juejin.im/entry/588ff7cd2f301e0069fd4fec

• https://codelabs.developers.google.com/codelabs/kotlin-
coroutines/#0

Coroutine