出于好奇,你如何避免涉及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
抽象来在一个线程上调度阻塞动作,但这仍然是一个很好的思想训练不是它..