The easist way (because extending from `AbstractCh...
# coroutines
h
The easist way (because extending from
AbstractChannel
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:
Copy code
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.
🧵 1