Scala - ScheduledFuture

前端 未结 7 1109
感动是毒
感动是毒 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 05:03

    Shortest solution for this, is probably making use of scala-async:

    import scala.async.Async.{async, await}
    
    def delay[T](value: T, t: duration): Future[T] = async {
      Thread.sleep(t.toMillis)
      value
    }
    

    Or in case you want delayed execution of a block

    def delay[T](t: duration)(block: => T): Future[T] async {
      Thread.sleep(t.toMillis)
      block()
    }
    

提交回复
热议问题