这是在scala控制台直接执行的例子。
import akka.actor._
import scala.concurrent.duration._
import scala.concurrent.Future val system = ActorSystem("mySystem")
import system.dispatcher Future {
var i:Int=0
var s:Cancellable=null
var running:Boolean=true s = system.scheduler.schedule(1 seconds, 1 seconds) { // 1st parameter, wait 1 sec then start the scheduler, and repeat every 1 second
i = i + 1
println("Test => " + i)
if(i>=5){
println("Have run enough times, end scheduler now.")
s.cancel() // cancel() to stop this scheduler
running=false
}
} println("===== Start schduler. =====")
while(running) Thread.sleep(3000) // This line is to keep the future body running, since scheduler will be async, so once
// scheduler is triggered and if without this line, the future will be completed immediately.
println("===== Stop schduler. =====") }.onComplete(_ => {
println("============== Completed ==============")
})