isClosedForReceive
Returns true
if the sending side of this channel was closed and all previously sent items were already received (which also happens for cancelled channels).
Note that if this property returns false
, it does not guarantee that a subsequent call to receive will succeed, as the channel can be concurrently cancelled or closed right after the check. For such scenarios, receiveCatching is the more robust solution: if the channel is closed, instead of throwing an exception, receiveCatching returns a result that allows querying it.
// DANGER! THIS CHECK IS NOT RELIABLE!
if (!channel.isClosedForReceive) {
channel.receive() // can still fail!
} else {
println("Can not receive: the channel is closed")
null
}
// DO THIS INSTEAD:
channel.receiveCatching().onClosed {
println("Can not receive: the channel is closed")
}.getOrNull()
Content copied to clipboard
The primary intended usage of this property is for assertions and diagnostics to verify the expected state of the channel. Using it in production code is discouraged.