[Kotlin] Coroutines

Darcy Daeseok YU ·2022년 12월 28일
0

KOTLIN : coroutines : asynchronous

launch : a coroutine builder that launches a new coroutine concurrently

runBlocking : a coroutine builder that bridges the non-coroutine world of regualar fun main()

fun main() = runBlocking { // this: CoroutineScope
    launch { // launch a new coroutine and continue
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    }
    println("Hello") // main coroutine continues while a previous one is delayed
}
//result :
//Hello
//World!

Structured concurrency : The above example shows that runBlocking establishes the corresponding scope and that is why the previous example waits until World! is printed after a second's delay and only then exits.

Extract function refactoring

fun main() = runBlocking { // this: CoroutineScope
    launch { doWorld() }
    println("Hello")
}

// this is your first suspending function
suspend fun doWorld() {
    delay(1000L)
    println("World!")
}

Scope builder :In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using the coroutineScope builder. It creates a coroutine scope and does not complete until all launched children complete.

  • runBlocking and coroutineScope builders may look similar because they both wait for their body and all its children to complete.

  • Main difference between runBlocking and coroutineScope
    runBlocking : Blocking the current thread for waiting,
    coroutineScope : A suspending function that suspends, releasing the underlying thread for other usages.

fun main() = runBlocking {
    doWorld()
}

suspend fun doWorld() = coroutineScope {  // this: CoroutineScope
    launch {
        delay(1000L)
        println("World!")
    }
    println("Hello")
}

A coroutineScope builder can be used inside any suspending function to perform multiple concurrent operations.

asynchronous

runBlocking {
	luach { // couroutine  비동기 
    	suspend fun(){ // 완료될때까지 기다림
        	delay(1000)
        	println("World!") // 1초뒤에 비동기 출력 
        }   
    } 
}
runBlocking {
	suspend fun() = coroutineScope {
    	launch{
        	delay(1000)
        	println("World!") // 1초뒤에 비동기 출력             
        }   
    }
}

coroutineScope concurrency (동시 실행)

// Sequentially executes doWorld followed by "Done"
fun main() = runBlocking {
    doWorld() // coroutine 처리 완료 될때 까지 기다림 (suspend) 
    println("Done") 
}

// Concurrently executes both sections
suspend fun doWorld() = coroutineScope { // this: CoroutineScope
    launch {
        delay(2000L)
        println("World 2") // 2초후 비동기 출력 
    }
    launch {
        delay(1000L)
        println("World 1") // 1초후 비동기 출력 
    }
    println("Hello") // 즉시 실행
}

Hello
World 1
World 2
Done

Synchronous

코루틴 함수 실행완료 후 진행
suspend fun () without launch(Asynchronout 생성)

fun main() = runBlocking { // this: CoroutineScope
    hello1()   
    println("Hello") // main coroutine continues while a previous one is delayed
}
suspend fun hello1(){
    delay(1000L) 
    println("Hello1!") // print after delay
}
// launch a new coroutine and keep a reference to its Job
val job = launch { 
    delay(1000L)
    println("World!")
}
println("Hello")
job.join() // wait until child coroutine completes
println("Done") 

job.join()으로 await 효과 (비동기->동기)

profile
React, React-Native https://darcyu83.netlify.app/

0개의 댓글