Coroutine
-
코루틴(Coroutine) 기초프로그래밍/Kotlin 2024. 1. 29. 16:22
코루틴을 사용하기 전 의존성을 추가해야한다. dendendcies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3") testImplementation(kotlin("test)) } runBlocking 코루틴을 생성하는 코루틴 빌더. runBlocking으로 감싼 코드는 코루틴 내부의 코드가 수행이 끝날 때까지 스레드가 블록킹된다. fun main() { runBlocking { println("hello") println(Thread.currentThread().name) } println("world") println(Thread.currentThread().name) } // Hello // main @coroutine#..
-
스프링 웹플럭스의 코루틴 지원Spring/Webflux 2024. 1. 29. 14:43
코루틴 코루틴(Coroutine)은 코틀린에서 비동기-논브로킹 프로그래밍을 명령어 스타일로 작성할 수 있도록 도와주는 라이브러리이다. 코루틴은 멀티 플랫폼을 지원하여 코틀린을 사용하는 안드로이드, 서버 등 여러 환경에서 사용할 수 있다. 코루틴은 일시 중단 가능한 함수(suspend function)을 통해 스레드가 실행을 잠시 중단했다가 중단한 지점부터 다시 재개(resume) 할 수 있다. 코루틴을 사용한 구조적 동시성 예시 suspend fun combineApi() = coroutineScope { val res1 = async { getApi1() } val res2 = async { getApi2() } return ApiResult ( res1.await() res2.await() ) } 리..