Hannes
02/27/2021, 11:01 AMAbstractChannel
doesn’t seem to be straightforward) I can think of is something like create my own Channel Alike implementation that uses a Channel
internally to signal that there is something to receice:
class PrioriytQueueChannel<T> (comparator : (T, T) -> Int : Channel {
private val buffer = PriorityQueue(comparator)
private val mutex : Mutex = ...
private val channel = Chanel<Unit>(Channel.UNLIMITED)
override suspend fun send(element : T){
mutex.withLock{
buffer.push(element)
}
channel.send(Unit) // wakeup receiver that buffer contains at least one element to receive
}
override suspend fun receive() : T {
channel.receive() // Wait until there is at least one element to receive
val element = mutex.withLock {
buffer.pull()
}
return element
}
}
One problem could be that buffer
could run out of capacity while channel
doesn’t but for my use case this seems to be very unlikely and I think can be ignored.