[#1246] Fix cpu-spinning regression in HashedWheelTimer

Also remove the usage of System.currentTimeMillis() completely here to make it more consistent and correct
This commit is contained in:
Norman Maurer 2013-04-08 07:01:08 +02:00
parent ded09fc778
commit 2508c76e97

View File

@ -300,7 +300,7 @@ public class HashedWheelTimer implements Timer {
@Override @Override
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
final long currentTime = System.currentTimeMillis(); final long currentTime = System.nanoTime();
if (task == null) { if (task == null) {
throw new NullPointerException("task"); throw new NullPointerException("task");
@ -311,9 +311,9 @@ public class HashedWheelTimer implements Timer {
start(); start();
delay = unit.toMillis(delay); long delayInNanos = unit.toNanos(delay);
HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delay); HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delayInNanos);
scheduleTimeout(timeout, delay); scheduleTimeout(timeout, delayInNanos);
return timeout; return timeout;
} }
@ -361,7 +361,7 @@ public class HashedWheelTimer implements Timer {
List<HashedWheelTimeout> expiredTimeouts = List<HashedWheelTimeout> expiredTimeouts =
new ArrayList<HashedWheelTimeout>(); new ArrayList<HashedWheelTimeout>();
startTime = System.currentTimeMillis(); startTime = System.nanoTime();
tick = 1; tick = 1;
while (workerState.get() == WORKER_STATE_STARTED) { while (workerState.get() == WORKER_STATE_STARTED) {
@ -444,7 +444,7 @@ public class HashedWheelTimer implements Timer {
long deadline = startTime + tickDuration * tick; long deadline = startTime + tickDuration * tick;
for (;;) { for (;;) {
final long currentTime = System.currentTimeMillis(); final long currentTime = System.nanoTime();
long sleepTimeMs = (deadline - currentTime + 999999) / 1000000; long sleepTimeMs = (deadline - currentTime + 999999) / 1000000;
if (sleepTimeMs <= 0) { if (sleepTimeMs <= 0) {
@ -539,7 +539,7 @@ public class HashedWheelTimer implements Timer {
@Override @Override
public String toString() { public String toString() {
long currentTime = System.currentTimeMillis(); final long currentTime = System.nanoTime();
long remaining = deadline - currentTime; long remaining = deadline - currentTime;
StringBuilder buf = new StringBuilder(192); StringBuilder buf = new StringBuilder(192);