2016-04-12 14 views
0

スカラーのカウントダウTimerはどのように実装できますか? 私は10秒前に数えてタイマーしたい、私の方法で続ける。スカラーのカウントダウンタイマーを実装する

私が見つけたすべてはこのStopwatchです:あなたはあなたのプログラムがそれをサポートするための特別な方法を構造化する必要があります適切な非ブロックの実行が必要な場合

パッケージビュー

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() 
    } 
} 
+0

もしあなたが望むのが遅れているのであれば、 'Thread.sleep'を見てください。 – Aivean

答えて

0

。あなたがアッカschedulerを使用するかどうか:

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

またはFuture/Promise - 完了したことはありません約束し、10秒の期間でそれを実行します。

もしこれが過剰で、ブロックの使用を気にしていないのであれば、Thread.sleep(10000) - あなたのスレッドはその時間に浪費されます。同様のブロッキング溶液は、Scala - ScheduledFutureである。

関連する問題