Skip to content

Instantly share code, notes, and snippets.

/* CoroutineScope 확장함수 produceSquares 선언
* : ReceiveChannel<Int>는 produce의 반환 인스턴스
* : produce{} 블록 내부가 송신자(Sender)가 됨
*/
fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
// 1~10까지 배수의 값을 전달
for (i in 1..10) send(i * i)
}
fun main() = runBlocking {
@posth2071
posth2071 / CoroutineBuilder_Actor Sample
Last active May 1, 2024 14:46
코루틴빌더(CoroutineBuilder) Actor 사용 예시
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.actor
import kotlin.system.measureTimeMillis
/*
* Sealed Class CounterMsg 정의
* : Sealed class는 enum class의 확장판 개념 (enum은 상수들의 집합이라면 sealed는 클래스들의 집합)
* : Sealed Class는 모두 abstract(추상) 상태로 사용 시 구현이 필요
* : 내부 클래스는 sealed class를 상속(extends) 필수, CounterMsg()
* */