oshai
03/12/2018, 9:58 AMJonathan
03/12/2018, 10:06 AM// 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)
}
}
}
oshai
03/12/2018, 11:17 AM