목록Mobile (128)
Hanbit the Developer
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html 멀티 쓰레드 병렬 처리 문제는, 주로 shared mutable state 동기화에서 발생한다. 해결 방안은 멀티쓰레드에서의 그것과 유사하다. The problem suspend fun massiveRun(action: suspend () -> Unit) { val n = 100 // number of coroutines to launch val k = 1000 // times an action is repeated by each corouti..
Kotlin Documentation 시리즈에 대해Category: Official libraries - Coroutines문서 링크: https://kotlinlang.org/docs/exception-handling.htmlException propagation코루틴 빌더는 두 유형으로 나뉜다: 예외를 자동으로 전파하는 방식(launch, actor), 예외를 유저에게 노출(async, produce)하는 방식.(https://chat.openai.com/share/295078a5-2ad9-428d-a3ff-44ac4deafe02) 코루틴 빌더가 루트 코루틴을 생성하는 데 쓰였다면, 전자에 해당하는 빌더는 예외를 uncaught exception으로 취급한다.@OptIn(DelicateCoroutin..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/channels.html Channel basics BlockingQueue(”쓰레드 세이프하게 원자를 추가 및 삭제하는 자료구조로, 큐가 꽉 차있거나 비었을 때 쓰레드를 block할 수 있다.” - ChatGPT)와 개념적으로 유사하다. 차이점은 suspending send 함수, suspending receive 함수를 가졌다는 점이다. val channel = Channel() launch { // this might be heavy CPU-consuming computation or async logic, we..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/flow.html Representing multiple values Sequences *remind: lazily하게 작동하는 방식의 컬렉션 → 각 작업이 짧지 않은 시간이 소요될 경우 사용 fun simple(): Sequence = sequence { // sequence builder for (i in 1..3) { Thread.sleep(100) // pretend we are computing it yield(i) // yield next value } } fun main() { simple().forEac..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html 코루틴은 언제나 여러 코루틴 컨택스트로 표현된 컨텍스트에서 실행되며, 코루틴 컨택스트는 Dispatcher, Job, CoroutineExceptionHandler, CoroutineName 등 여러 요소의 집합이다. Dispatchers and threads CoroutineDispatcher는 어떤 쓰레드가 실행되는 데 사용되는지를 결정한다. 코루틴 디스패처는 코루틴 실행을 특정 쓰레드로 제한하거나, 쓰레드 풀 내에서 디스패치를 하거나, 제한 없..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/composing-suspending-functions.html Sequential by default fun main() = runBlocking { val time = measureTimeMillis { val one = doSomethingUsefulOne() val two = doSomethingUsefulTwo() println("The answer is ${one + two}") } println("Completed in $time ms") } suspend fun doSomethingUsefulOne():..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/cancellation-and-timeouts.html val job = launch { repeat(1000) { i -> println("job: I'm sleeping $i ...") delay(500L) } } delay(1300L) // delay a bit println("main: I'm tired of waiting!") job.cancel() // cancels the job job.join() // waits for job's completion println("main: Now I can quit...
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/coroutines-and-channels.html Concurrency async: 새 코루틴을 실행하고 Deferred 객체(a.k.a Future or Promise)를 반환한다. interface Deferred : Job launch: 새 코루틴을 실행하고 Job 객체를 반환한다. Job 객체를 통해 코루틴을 중단하거나 결과를 기다릴 수 있다(start(), join(), cancel(), cancelAndJoin(), cancelChildren()) Channels interface SendChannel {..
Kotlin Documentation 시리즈에 대해 Category: Official libraries - Coroutines 문서 링크: https://kotlinlang.org/docs/coroutines-basics.html Your first coroutine 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..
Kotlin Documentation 시리즈에 대해 Category: Standard library 문서 링크: https://kotlinlang.org/docs/opt-in-requirements.html API가 실험적이거나 바뀔 예정이라면 opt-in을 붙여 API 사용자에게 이를 알릴 수 있다. Opt in to using API Propagating opt-in opt-in requirement annotation을 통해 opt-in을 지정할 수 있다. 이때 opt-in이 전파되기 때문에 관련 함수 등에 모두 어노테이션을 달아야 한다. // Library code @RequiresOptIn(message = "This API is experimental. It may be changed in t..