BUFFERED
A channel capacity marker that is substituted by the default buffer capacity.
When passed as a parameter to the Channel(...)
factory function, the default buffer capacity is used. For BufferOverflow.SUSPEND (the default buffer overflow strategy), the default capacity is 64, but on the JVM it can be overridden by setting the DEFAULT_BUFFER_PROPERTY_NAME system property. The overridden value is used for all channels created with a default buffer capacity, including those created in third-party libraries.
val channel = Channel<Int>(Channel.BUFFERED)
repeat(100) {
channel.trySend(it)
}
channel.close()
// The check can fail if the default buffer capacity is changed
check(channel.toList() == (0..<64).toList())
Content copied to clipboard
If a different BufferOverflow is specified, Channel(BUFFERED)
creates a channel with a buffer of size 1:
val channel = Channel<Int>(Channel.BUFFERED, onBufferOverflow = BufferOverflow.DROP_OLDEST)
channel.send(1)
channel.send(2)
channel.send(3)
channel.close()
check(channel.toList() == listOf(3))
Content copied to clipboard