Kotlin 코루틴: 비동기 프로그래밍
코루틴은 Kotlin에서 비동기 프로그래밍을 위한 강력한 도구입니다. 가벼운 스레드라고 생각할 수 있습니다.
설정
Gradle 설정
apply plugin: 'kotlin'
// 또는
apply plugin: 'kotlin-android'
kotlin {
experimental {
coroutines 'enable'
}
}
repositories {
jcenter()
}
dependencies {
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16"
}
기본 개념
suspend 함수
suspend 키워드는 함수가 일시 중단될 수 있음을 나타냅니다.
suspend fun longRunningTask(): Long {
val time = measureTimeMillis {
println("Please wait")
delay(2, TimeUnit.SECONDS) // 2초 대기
println("Delay Over")
}
return time
}
runBlocking
다른 스레드에서 작업하지만 현재 스레드를 블록합니다.
fun main(args: Array<String>) {
runBlocking {
val exeTime = longRunningTask()
println("Execution Time is $exeTime")
}
}
delay
스레드를 블록하지 않고 일시 중단합니다. suspend 함수 내에서만 사용 가능합니다.
runBlocking {
delay(5, TimeUnit.SECONDS)
}
async
비동기 작업을 시작하고 결과를 나중에 받을 수 있습니다.
Context
Unconfined: 메인 스레드CommonPool: 공용 스레드 풀
사용법
fun main(args: Array<String>) {
val time = async(CommonPool) { longRunningTask() } // 비동기 시작
println("Print after async")
runBlocking {
println("printing time ${time.await()}") // 결과 대기
}
}
await와 예외 처리
await() 호출 전까지 예외는 무시됩니다. 예외를 확인하려면 await()를 호출해야 합니다.
val deferred = async(CommonPool) {
throw RuntimeException("Error!")
}
// 여기서는 예외가 발생하지 않음
println("After async")
// await() 호출 시 예외 발생
try {
deferred.await()
} catch (e: Exception) {
println("Exception caught: ${e.message}")
}
시간 측정
measureTimeMillis로 람다 실행 시간을 측정할 수 있습니다.
val time = measureTimeMillis {
// 측정할 코드
performOperation()
}
println("Took $time ms")
유용한 리소스
다음 단계
코루틴의 기본에 대해 알아보았습니다. 다음으로 Kotlin Native 동시성에 대해 알아보세요.
Comments