allows-buffering

Code


fun main(args: Array<String>) {
  runBlocking {
    val channel = Channel<Int>(Channel.UNLIMITED)

    val sender = launch(CoroutineName("${Emoji.LETTER}-sender")) {
      repeat(5) {
        delay(50)

        out.info("starting_to_send: $it")
        channel.send(it)
        out.info("sent: $it")
      }
    }

    val listener = launch(CoroutineName("${Emoji.MAILBOX}-listener")) {
      repeat(2) {
        delay(500)

        out.info("received: ${channel.receive()}")
      }
    }

    delay(3000)
    listener.cancel()
    sender.cancel()
    out.info("Main completed")
  }
}

Command to reproduce:

gt.sandbox.checkout.commit e067a37e2aa019ce5451 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run --quiet"

Recorded output of command:

[elapsed:  100ms][coroutine:✉️-sender] starting_to_send: 0
[elapsed:  116ms][coroutine:✉️-sender] sent: 0
[elapsed:  170ms][coroutine:✉️-sender] starting_to_send: 1
[elapsed:  170ms][coroutine:✉️-sender] sent: 1
[elapsed:  226ms][coroutine:✉️-sender] starting_to_send: 2
[elapsed:  226ms][coroutine:✉️-sender] sent: 2
[elapsed:  279ms][coroutine:✉️-sender] starting_to_send: 3
[elapsed:  279ms][coroutine:✉️-sender] sent: 3
[elapsed:  334ms][coroutine:✉️-sender] starting_to_send: 4
[elapsed:  334ms][coroutine:✉️-sender] sent: 4
[elapsed:  542ms][coroutine:📬-listener] received: 0
[elapsed: 1048ms][coroutine:📬-listener] received: 1
[elapsed: 3043ms][coroutine:unnamed] Main completed

Backlinks