Hannes
02/27/2021, 9:43 AMChannel with Channel.UNLIMITED capacity
that has PriorityQueue
alike behavior under the hood, meaning if there are elements in the buffer waiting to be received whenever a new element is send it will be moved not necessarily to the end of the buffer / queue but rather at the “right position given this elements priority”?
i.e.
Lets Say I have something like
data class Input( data : String, priority: Int) // a higher number for priority means higher priority
val myChannel = Channel<Input>( Channel.UNLIMITED ) // but somehow internally it should order waiting elements by Input.priority
So now when I send elements the order in which receive
gets elements our of the buffer is different then the order of sending: i.e.
myChannel.send( Input("foo", 1) )
myChannel.send( Input("blabla", 1) )
myChannel.send( Input("foo", 1))
myChannel.send( Input("High prio", 100 ) )
myChannel.send( Input("something", 1) )
Then I would like to see the buffer be sorted like this
Input("High prio", 100 )
Input("foo", 1)
Input("foo", 1)
Input("something", 1)
and of course it should also work like that when the channel is receiving elements already
val myChannel = Channel<Input>( Channel.UNLIMITED )
launch {
myChannel.send( Input("foo", 1) )
myChannel.send( Input("blabla", 1) )
myChannel.send( Input("foo", 1))
myChannel.send( Input("High prio", 100 ) )
myChannel.send( Input("something", 1) )
}
launch {
while (true){
val element = myChannel.receive()
delay(100)
println(element)
}
}
then the output should be:
Input(“foo”, 1) // as it was the first one to be received
Input(“High prio”, 100 ) // As the next one pulled out of the channel’s buffer but buffer was sorted by priority
Input(“blabla”, 1)
Input(“foo”, 1)
Input(“something”, 1)Is something like a priority queue as buffer for Channels already build in somehow and I have just missed it?