Use nanoTime in FixedRateTask, see #2214

* Rewrote test to use latch and assert rate instead
This commit is contained in:
Patrik Nordwall 2012-06-12 13:37:21 +02:00
parent b27bae6554
commit 7b6ae2f5c9
2 changed files with 21 additions and 17 deletions

View file

@ -29,11 +29,10 @@ private[akka] object FixedRateTask {
*/
private[akka] class FixedRateTask(scheduler: Scheduler, initalDelay: Duration, delay: Duration, task: Runnable) extends Runnable {
private val delayMillis = delay.toMillis
private val minDelayMillis = 1L
private val delayNanos = delay.toNanos
private val cancelled = new AtomicBoolean(false)
private val counter = new AtomicLong(0L)
private val startTime = System.currentTimeMillis + initalDelay.toMillis
private val startTime = System.nanoTime + initalDelay.toNanos
scheduler.scheduleOnce(initalDelay, this)
def cancel(): Unit = cancelled.set(true)
@ -41,11 +40,9 @@ private[akka] class FixedRateTask(scheduler: Scheduler, initalDelay: Duration, d
override final def run(): Unit = if (!cancelled.get) try {
task.run()
} finally if (!cancelled.get) {
val nextTime = startTime + delayMillis * counter.incrementAndGet
val nextDelayMillis = nextTime - System.currentTimeMillis
val nextDelay = Duration(
(if (nextDelayMillis <= minDelayMillis) minDelayMillis else nextDelayMillis),
TimeUnit.MILLISECONDS)
val nextTime = startTime + delayNanos * counter.incrementAndGet
// it's ok to schedule with negative duration, will run asap
val nextDelay = Duration(nextTime - System.nanoTime, TimeUnit.NANOSECONDS)
try {
scheduler.scheduleOnce(nextDelay, this)
} catch { case e: IllegalStateException /* will happen when scheduler is closed, nothing wrong */ }