Actor always wake up as soon as possible after receiving an element.
Copy code
// take care of thread safety of the queue
val messages: Queue
// Create an actor design to receive "wake-up" message (Unit can be used as an element type)
// Conflated buffer is usefull to not buffer "wake-up" messages if the process is longuer than the time between two wake-ups
val actor = actor<Unit>(capacity = Channel.CONFLATED) {
consumeEach {
// process pending messages
}
}
init {
launch {
while(isActive) {
// wait 2 seconds
delay(2, TimeUnit.SECONDS)
// awake the actor
actor.send(Unit)
}
}
}