Scala - ScheduledFuture

前端 未结 7 1123
感动是毒
感动是毒 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:02

    You could change your code to something like this:

    val d = 5.seconds.fromNow
    val f = Future {delay(d); 1}
    val res = Await.result(f, Duration.Inf)
    
    def delay(dur:Deadline) = {
      Try(Await.ready(Promise().future, dur.timeLeft))
    }
    

    But I would not recommend it. In doing so, you would be blocking in a Future (blocking to wait for that Promise that will never complete), and I think blocking in the ExecutionContext is greatly discouraged. I would either look into using the java scheduled executor as you stated or you could look into using Akka as @alex23 recommended.

提交回复
热议问题