jkbbwr
04/13/2017, 6:17 PM// Message types for counterActor
sealed class CounterMsg
object IncCounter : CounterMsg() // one-way message to increment counter
class GetCounter(val response: SendChannel<Int>) : CounterMsg() // a request with reply
// This function launches a new counter actor
fun counterActor() = actor<CounterMsg>(CommonPool) {
var counter = 0 // actor state
for (msg in channel) { // iterate over incoming messages
when (msg) {
is IncCounter -> counter++
is GetCounter -> msg.response.send(counter)
}
}
}
fun main(args: Array<String>) = runBlocking<Unit> {
val counter = counterActor() // create the actor
massiveRun(CommonPool) {
counter.send(IncCounter)
}
val response = Channel<Int>()
counter.send(GetCounter(response))
println("Counter = ${response.receive()}")
counter.close() // shutdown the actor
}