Channel 은 서로 다른 코루틴 간의 통신(데이터 교환)을 할 수 있는 메커니즘임.

Channels 기초

BlockingQueue란?
Java의 동시성 패키지에 포함된 인터페이스로, 스레드 간에 안전하게 항목을 추가하거나 제거할 수 있는 대기열(queue)을 제공함.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel

fun main(args: Array<String>) = runBlocking<Unit> {
    val channel = Channel<Int>()
    launch { for(x in 1..5) channel.send(x) }
    repeat(5) { println(channel.receive()) }
    println("Done!")
}

// 실행결과 
1
2
3
4
5
Done!
interface Channel<E> : SendChannel<E> , ReceiveChannel<E>

Channel 닫기와 반복적으로 수신하기