akka调度程序避免递归

时间:2016-07-14 11:32:30

标签: scala recursion akka

出于好奇,你如何避免涉及Future返回类型的无限递归,如本例所示?递归应该无限,但这又如何避免累积无限数量的线程?

  import scala.concurrent.duration._
  import scala.concurrent.blocking
  import scala.concurrent.Future
  import scala.concurrent.ExecutionContext.Implicits.global

  val system = ActorSystem("MySystem") // make global if more things hinge on the actor system

  val interval = Configuration.dbSyncIntervalSecs

  private def acquireLoop: Future[Any] = {
    Future { 
      blocking { 
        println("about to sync from database")
        acquire
        system.scheduler.scheduleOnce(interval seconds)(acquireLoop)
      }
    }
  }

我想以某种方式压扁这一点,但@tailrec不适用。也许我不应该使用Future抽象来在一个线程上调度阻塞动作,但这仍然是一个很好的思想训练不是它..

0 个答案:

没有答案