Composing suspending functions • Coroutine context and dispatchers • Exception handling and supervision • Channels (experimental) • Shared mutable state and concurrency • Select expression (experimental) https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html Select expressions are an experimental feature of kotlinx.coroutines. Their API is expected to evolve in the upcoming updates of the kotlinx.coroutines library with potentially breaking changes.
delay(200L) println( "Task from runBlocking") } coroutineScope { // Creates a new coroutine scope launch { delay(500L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") // This line will be printed before nested launch } println("Coroutine scope is over") // This line is not printed until nested launch completes } println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0].toString() + "Task from runBlocking") println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0]).toString()+"Task from nested launch") println(Exception().stackTrace[0]) println(Exception().stackTrace[0].toString()+"Task from coroutine scope") //This line will be printed before nested launch println(Exception().stackTrace[0].toString()+"Coroutine scope is over") //This line is not printed until nested launch completes 학습팁 1 - 실행 순서 추적하기
delay(200L) println( "Task from runBlocking") } coroutineScope { // Creates a new coroutine scope launch { delay(500L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") // This line will be printed before nested launch } println("Coroutine scope is over") // This line is not printed until nested launch completes } println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0].toString() + "Task from runBlocking") println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0]) println(Exception().stackTrace[0]).toString()+"Task from nested launch") println(Exception().stackTrace[0]) println(Exception().stackTrace[0].toString()+"Task from coroutine scope") // This line will be printed before nested launch println(Exception().stackTrace[0].toString()+"Coroutine scope is over") // This line is not printed until nested launch completes 학습팁 1 - 실행 순서 추적하기 main$1.invokeSuspend(Scopebuilder.kt:5) main$1.invokeSuspend(Scopebuilder.kt:11) main$1$2.invokeSuspend(Scopebuilder.kt:14) main$1$2.invokeSuspend(Scopebuilder.kt:21) main$1$1.invokeSuspend(Scopebuilder.kt:7) main$1$2$1.invokeSuspend(Scopebuilder.kt:16) main$1$2.invokeSuspend(Scopebuilder.kt:24)Task from coroutine scope main$1$1.invokeSuspend(Scopebuilder.kt:9)Task from runBlocking main$1$2$1.invokeSuspend(Scopebuilder.kt:18) main$1$2$1.invokeSuspend(Scopebuilder.kt:19) main$1.invokeSuspend(Scopebuilder.kt:27)Coroutine scope is over
{ println(Exception().stackTrace[0]) delay(200L) println(Exception().stackTrace[0].toString() + "Task from runBlocking") } println(Exception().stackTrace[0]) coroutineScope { // Creates a new coroutine scope println(Exception().stackTrace[0]) launch { println(Exception().stackTrace[0]) delay(500L) println(println(Exception().stackTrace[0]).toString()+"Task from nested launch") } println(Exception().stackTrace[0]) delay(100L) println(Exception().stackTrace[0].toString()+"Task from coroutine scope") // This line will be printed before nested launch } println(Exception().stackTrace[0].toString()+"Coroutine scope is over") // This line is not printed until nested launch completes } 학습팁 1 - 실행 순서 추적하기(전체코드)
fun main() = runBlocking(CoroutineName("main")) { log("Started main coroutine") // run two background value computations val v1 = async(CoroutineName("v1coroutine")) { delay(500) log("Computing v1") 252 } ... } [main @main#1] Started main coroutine [main @v1coroutine#2] Computing v1 [main @v2coroutine#3] Computing v2 Process finished with exit code 0
on JVM. It falls into a different category, though. There is no byte-code manipulation involved and it works on Android, too. However, you will have to write your coroutines in Kotlin. The upside is that Kotlin is designed for interoperability with Java in mind, so you can still continue to use all your Java libraries and freely combine Kotlin and Java code in the same project, even putting them side-by-side in the same directories and packages. This Guide to kotlinx.coroutines provides many more examples, while the coroutines design document explains all the motivation, use-cases and implementation details. https://stackoverflow.com/a/43228137 Roman Elizarov
must fail`() { println("main thread start") asyndFun { println("main thread check assert") //이 로그는 확인 할 수가 없어요 assertEquals(3, it) } println("main thread end") } Bad main thread start main thread end work thread start Process finished with exit code 0
must success`() { println("main thread start") var result = -1 asyndFun { println("main thread check assert") //이 로그는 확인 할 수가 없어요 result = it } assertEquals(4, result) println("main thread end") } Bad
must success`() { println("main thread start") var result = -1 asyndFun { println("main thread check assert") //이 로그는 확인 할 수가 없어요 result = it } assertEquals(4, result) println("main thread end") } Bad main thread start work thread start java.lang.AssertionError: Expected :4 Actual :-1
println("main thread start") val channel = Channel<Int>() asyncFun { launch { channel.send(it) } } println("main thread check assert") val result = channel.receive() assertEquals(4, result) println("main thread end") } After main thread start main thread check assert work thread start work thread end main thread end
{ if (!rootingChecked) { step00RootingCheck() return } if (!v3Checked) { step01v3Check() return } 뱅크 시작 시 보안 처리 Before fun step00RootingCheck() { //doSomeThing rootingCheckedr = true load() } fun step01v3Check() { //doSomeThing v3Checked = true load() }
async { step04gcmCheck() } val deferredBanner = async { step06assetBannerCheck() } step07v3UpdateCheck() step11noticeCheck() step12checkSSO() deferredGcm.await() deferredBanner.await() } After 뱅크 시작 시 보안 처리
https://medium.com/@myungpyo/reading-coroutine-official-guide-thoroughly-part-0-20176d431e9d ◦ https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/README.md • KotlinConf 2017 - Deep Dive into Coroutines on JVM by Roman Elizarov ◦ https://youtu.be/YrrUCSi72E8 • Kotlin의 Coroutine은 어떻게 동작하는가 ◦ https://medium.com/til-kotlin-ko/kotlin의-coroutine은-어떻게-동작하는가-789291da6a50 • Android Suspenders (Android Dev Summit '18) ◦ https://youtu.be/EOjq4OIWKqM • 코루틴 동작 설명 영상 ◦ https://youtu.be/EOjq4OIWKqM?t=877 • CallbackHell 영상 ◦ https://youtu.be/4W3ruTWUhpw?t=213 참고자료
half of its firepower. RxJava는 바주카포와 같으며 대부분의 응용 프로그램은 화력의 절반도 사용하지 않습니다. https://proandroiddev.com/i-exchanged-rxjava-for-coroutines-in-my-android-application-why-you-probably-should-do-the-same-5526dfb38d0e RxJava coroutines 출처 나무위키
Coroutines, Type aliases and more #230 A glimpse of Async-Await on Android #246 kotlin-coroutines-retrofit #247 Kotlin 1.1 Released with JavaScript Support, Coroutines and more #249 Why Im Skeptical about Kotlin Coroutines #262 A first walk into Kotlin coroutines on Android #274 Rewriting RxJava with Kotlin Coroutines? #278 Create a Clean-Code App #279 Diving deep into Kotlin Coroutines #282 Clean App with Kotlin & Architecture Components - Part 2 #285 Clean-Code App with Kotlin and Architecture Components ? Part 3 #290 retrofit2-kotlin-coroutines-adapter #293 Simple asynchronous loading with Kotlin Coroutines #303 Android Coroutine Recipes #303 Kotlin coroutines vs RxJava: an initial performance test #304 Coroutines and RxJava???An Asynchronicity Comparison (Part 1)
Asynchronicity Comparison (Part 4) #308 Even smarter async with coroutine actors #308 Kotlin Coroutines — Handling concurrency like a pro with Retrofit 2 #308 RxJava to Kotlin coroutines #309 Coroutines and RxJava? (Part 5): Operators #319 From Java to Kotlin beyond Alt+Shift+Cmd+K #320 Simple network calls using Retrofit, LiveData, Kotlin Coroutines and DSL #324 Forget RxJava: Kotlin Coroutines are all you need. Part 1/2 #325 A major release just around the corner ? meet Kotlin 1.3-M2 #325 Announcing Okio 2: Our fast + simple I/O library #325 I exchanged RxJava for coroutines in my Android application #329 Demystifying CoroutineContext #330 Unidirectional Data Flow using Coroutines #331 Explore How Kotlin Coroutine Works Under The Hood #331 Kotlin Coroutines Introduction and Guide
? Part 1 #332 kotlinx.coroutines 1.0.0-RC1 #333 Kotlin Coroutines in action #333 Part 2???Coroutine Cancellation and Structured Concurrency #334 Android Networking with Coroutines and Retrofit #334 Blocking threads, suspending coroutines #334 Kotlin 1.3 Released #334 Using Kotlin Coroutines in your Android App #335 Kotlin Coroutines Concurrency #336 Migrate your Android project to Kotlin 1.3 #337 Black Friday deals at raywenderlich.com #337 Playing With Kotlin Coroutines For Best Practices #339 Chris Banes: Android Suspenders #339 Kotlin Coroutines patterns & anti-patterns #339 Level Up in Kotlin #342 Coroutines and Idling Resources
and Channels #347 Android Team/Tech Lead ? ?? ?? #347 Kotlin Coroutines Cheat Sheet #348 Room & Coroutines #349 Launching a Kotlin Coroutine for immediate execution #349 Retrofit meets coroutines #352 Coroutine Support in ViewModels #354 Easy Coroutines in Android: viewModelScope #355 Kotlin Coroutines: A Case Study, Part 1