如何在 Scala 中的语句之间等待 N 秒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35518759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 08:02:00 来源:igfitidea点击:
How to wait for N seconds between statements in Scala?
提问by Mamun
I have two statements like this:
我有两个这样的声明:
val a = 1
val b = 2
In between the 2 statements, I want to pause for N seconds like I can in bashwith sleepcommand.
在这两个语句之间,我想暂停 N 秒,就像我可以bash使用sleep命令一样。
回答by Carson Pun
You can try:
你可以试试:
val a = 1
Thread.sleep(1000) // wait for 1000 millisecond
val b = 2
You can change 1000 to other values to accommodate to your needs.
您可以将 1000 更改为其他值以满足您的需要。
回答by som-snytt
Given:
鉴于:
package object wrap {
import java.time._
def delayed[A](a: => A): A = {
Console println Instant.now
Thread.sleep(1000L)
val x = a
Console println Instant.now
x
}
}
You can:
你可以:
Welcome to Scala 2.12.0-M3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.
scala> $intp.setExecutionWrapper("wrap.delayed")
scala> { println("running"); 42 }
2016-02-20T06:28:17.372Z
running
2016-02-20T06:28:18.388Z
res1: Int = 42
scala> :quit

