这是在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 ==============")
})

 

相关文章:

  • 2022-01-04
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2022-01-03
  • 2021-11-11
猜你喜欢
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
相关资源
相似解决方案