根据配置在运行时添加功能

时间:2015-04-02 10:50:53

标签: scala

下面的代码连续打印4个语句,然后使用println的函数和自定义版本在后续4个语句之间暂停1秒:

object GoSlow extends App{


  println("step 1")  
  println("step 2") 
  println("step 3") 
  println("step 4")

  def f : Unit = Thread.sleep(1000)

  customPrintln("step 1 with function" , f)
  customPrintln("step 2 with function" , f)
  customPrintln("step 3 with function" , f)
  customPrintln("step 4 with function" , f)

  def customPrintln(x : Any , f : Unit) = {
    println(x) 
    f
  }


}

可以在代码中的指定位置注入Thread.sleep而不是使用自定义println函数(不使用反射)?还是一种减慢特定代码区域执行速度的模式?我试图在特定时间暂停执行代码。但是在大多数情况下,应该隐藏Thread.sleeps。 可以利用特征来实现这种行为吗?

0 个答案:

没有答案
相关问题