From 2508c76e97b40ac8aa14fa364f462c930079a437 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 8 Apr 2013 07:01:08 +0200 Subject: [PATCH] [#1246] Fix cpu-spinning regression in HashedWheelTimer Also remove the usage of System.currentTimeMillis() completely here to make it more consistent and correct --- .../main/java/io/netty/util/HashedWheelTimer.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/io/netty/util/HashedWheelTimer.java b/common/src/main/java/io/netty/util/HashedWheelTimer.java index 5ad882a266..dbfd95cb23 100644 --- a/common/src/main/java/io/netty/util/HashedWheelTimer.java +++ b/common/src/main/java/io/netty/util/HashedWheelTimer.java @@ -300,7 +300,7 @@ public class HashedWheelTimer implements Timer { @Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { - final long currentTime = System.currentTimeMillis(); + final long currentTime = System.nanoTime(); if (task == null) { throw new NullPointerException("task"); @@ -311,9 +311,9 @@ public class HashedWheelTimer implements Timer { start(); - delay = unit.toMillis(delay); - HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delay); - scheduleTimeout(timeout, delay); + long delayInNanos = unit.toNanos(delay); + HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delayInNanos); + scheduleTimeout(timeout, delayInNanos); return timeout; } @@ -361,7 +361,7 @@ public class HashedWheelTimer implements Timer { List expiredTimeouts = new ArrayList(); - startTime = System.currentTimeMillis(); + startTime = System.nanoTime(); tick = 1; while (workerState.get() == WORKER_STATE_STARTED) { @@ -444,7 +444,7 @@ public class HashedWheelTimer implements Timer { long deadline = startTime + tickDuration * tick; for (;;) { - final long currentTime = System.currentTimeMillis(); + final long currentTime = System.nanoTime(); long sleepTimeMs = (deadline - currentTime + 999999) / 1000000; if (sleepTimeMs <= 0) { @@ -539,7 +539,7 @@ public class HashedWheelTimer implements Timer { @Override public String toString() { - long currentTime = System.currentTimeMillis(); + final long currentTime = System.nanoTime(); long remaining = deadline - currentTime; StringBuilder buf = new StringBuilder(192);