0

How can implement count dow Timer on scala ? All i want to timer that count back 10 seconds before continue in my method.

All i found is this Stopwatch:

package views

class Stopwatch {
  var start = System.currentTimeMillis()
  def elapsed = System.currentTimeMillis() - start
  def printElapsed(msg: String) = {
    val delta = elapsed
    new Thread(new Runnable {
      def run = {
        System.out.println(msg + " time ellapsed: " + delta + "ms")
      }
    }).start()
  }
  def restart = {
    start = System.currentTimeMillis()
      new Thread(new Runnable {
      def run = {
        System.out.println("------restart------")
      }
    }).start()
  }
}
david hol
  • 1,272
  • 7
  • 22
  • 44

1 Answers1

0

If you want proper non-blocking execution your program will have to be structured special way to support it. Whether you use Akka scheduler:

system.scheduler.scheduleOnce(50 milliseconds) { 
  // call the rest of your method
}

or Future/Promise - a promise that never completes and you execute it with duration of 10 seconds.

If that's an overkill and you don't mind blocking use Thread.sleep(10000) - your thread will be wasted for that time. Similar blocking solution is: Scala - ScheduledFuture.

Community
  • 1
  • 1
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65