2016-02-20 83 views

回答

14

你可以試試:

val a = 1 
Thread.sleep(1000) // wait for 1000 millisecond 
val b = 2 

你可以改變1000到其他值,以適應您的需求。

5

考慮:

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 
    } 
} 

您可以:

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