Scala - ScheduledFuture

前端 未结 7 1107
感动是毒
感动是毒 2020-12-08 04:27

I am trying to implement scheduled future in Scala. I would like it to wait specific time and then execute the body. So far I tried the following, simple approach

         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 04:57

    If you want schedule the completion without Akka, you can use a regular Java Timer to schedule a promise to complete:

    def delay[T](delay: Long)(block: => T): Future[T] = {
      val promise = Promise[T]()
      val t = new Timer()
      t.schedule(new TimerTask {
        override def run(): Unit = {
          promise.complete(Try(block))
        }
      }, delay)
      promise.future
    }
    

提交回复
热议问题