2009-01-19 16:05:04 +01:00
|
|
|
/*
|
|
|
|
* JBoss, Home of Professional Open Source
|
|
|
|
*
|
|
|
|
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
|
|
*/
|
2009-01-20 05:37:26 +01:00
|
|
|
package org.jboss.netty.handler.timeout;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2009-01-20 08:57:45 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2009-01-19 16:05:04 +01:00
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
|
2009-01-20 05:39:11 +01:00
|
|
|
import org.jboss.netty.logging.InternalLogger;
|
|
|
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
2009-01-20 08:57:45 +01:00
|
|
|
import org.jboss.netty.util.ConcurrentIdentityHashMap;
|
2009-01-20 05:37:26 +01:00
|
|
|
import org.jboss.netty.util.ExecutorUtil;
|
|
|
|
import org.jboss.netty.util.MapBackedSet;
|
2009-01-20 08:57:45 +01:00
|
|
|
import org.jboss.netty.util.ReusableIterator;
|
2009-01-20 05:37:26 +01:00
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
/**
|
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*/
|
|
|
|
public class HashedWheelTimer implements Timer {
|
|
|
|
|
2009-01-20 05:39:11 +01:00
|
|
|
static final InternalLogger logger =
|
|
|
|
InternalLoggerFactory.getInstance(HashedWheelTimer.class);
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
final Executor executor;
|
|
|
|
final Worker worker = new Worker();
|
|
|
|
final AtomicInteger activeTimeouts = new AtomicInteger();
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
final long tickDuration;
|
2009-01-20 08:57:45 +01:00
|
|
|
final long roundDuration;
|
2009-01-19 16:05:04 +01:00
|
|
|
final Set<HashedWheelTimeout>[] wheel;
|
2009-01-20 08:57:45 +01:00
|
|
|
final ReusableIterator<HashedWheelTimeout>[] iterators;
|
2009-01-19 16:05:04 +01:00
|
|
|
final int mask;
|
|
|
|
final ReadWriteLock lock = new ReentrantReadWriteLock();
|
|
|
|
volatile int wheelCursor;
|
|
|
|
|
|
|
|
public HashedWheelTimer(Executor executor) {
|
2009-01-20 08:57:45 +01:00
|
|
|
this(executor, 100, TimeUnit.MILLISECONDS, 512); // about 50 sec
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public HashedWheelTimer(
|
|
|
|
Executor executor,
|
|
|
|
long tickDuration, TimeUnit unit, int ticksPerWheel) {
|
|
|
|
|
|
|
|
if (executor == null) {
|
|
|
|
throw new NullPointerException("executor");
|
|
|
|
}
|
|
|
|
if (unit == null) {
|
|
|
|
throw new NullPointerException("unit");
|
|
|
|
}
|
|
|
|
if (tickDuration <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"tickDuration must be greater than 0: " + tickDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.executor = executor;
|
|
|
|
|
|
|
|
// Normalize ticksPerWheel to power of two and initialize the wheel.
|
|
|
|
wheel = createWheel(ticksPerWheel);
|
2009-01-20 08:57:45 +01:00
|
|
|
iterators = createIterators(wheel);
|
2009-01-19 16:05:04 +01:00
|
|
|
mask = wheel.length - 1;
|
|
|
|
|
|
|
|
// Convert checkInterval to nanoseconds.
|
|
|
|
this.tickDuration = tickDuration = unit.toNanos(tickDuration);
|
|
|
|
|
|
|
|
// Prevent overflow.
|
|
|
|
if (tickDuration == Long.MAX_VALUE ||
|
|
|
|
tickDuration >= Long.MAX_VALUE / wheel.length) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"tickDuration is too long: " +
|
|
|
|
tickDuration + ' ' + unit);
|
|
|
|
}
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
roundDuration = tickDuration * wheel.length;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Set<HashedWheelTimeout>[] createWheel(int ticksPerWheel) {
|
|
|
|
if (ticksPerWheel <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
|
|
|
}
|
|
|
|
if (ticksPerWheel > 1073741824) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"ticksPerWheel may not be greater than 2^30: " + ticksPerWheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticksPerWheel = normalizeTicksPerWheel(ticksPerWheel);
|
2009-01-20 08:57:45 +01:00
|
|
|
Set<HashedWheelTimeout>[] wheel = new Set[ticksPerWheel];
|
|
|
|
for (int i = 0; i < wheel.length; i ++) {
|
|
|
|
wheel[i] = new MapBackedSet<HashedWheelTimeout>(new ConcurrentIdentityHashMap<HashedWheelTimeout, Boolean>());
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
return wheel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static ReusableIterator<HashedWheelTimeout>[] createIterators(Set<HashedWheelTimeout>[] wheel) {
|
|
|
|
ReusableIterator<HashedWheelTimeout>[] iterators = new ReusableIterator[wheel.length];
|
|
|
|
for (int i = 0; i < wheel.length; i ++) {
|
|
|
|
iterators[i] = (ReusableIterator<HashedWheelTimeout>) wheel[i].iterator();
|
|
|
|
}
|
|
|
|
return iterators;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int normalizeTicksPerWheel(int ticksPerWheel) {
|
|
|
|
int normalizedTicksPerWheel = 1;
|
|
|
|
while (normalizedTicksPerWheel < ticksPerWheel) {
|
|
|
|
normalizedTicksPerWheel <<= 1;
|
|
|
|
}
|
|
|
|
return normalizedTicksPerWheel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void releaseExternalResources() {
|
|
|
|
ExecutorUtil.terminate(executor);
|
|
|
|
}
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
public Timeout newTimeout(TimerTask task, long initialDelay, TimeUnit unit) {
|
|
|
|
if (task == null) {
|
|
|
|
throw new NullPointerException("task");
|
|
|
|
}
|
|
|
|
if (unit == null) {
|
|
|
|
throw new NullPointerException("unit");
|
|
|
|
}
|
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
initialDelay = unit.toNanos(initialDelay);
|
|
|
|
checkDelay(initialDelay);
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
// Add the timeout to the wheel.
|
2009-01-19 16:05:04 +01:00
|
|
|
HashedWheelTimeout timeout;
|
|
|
|
lock.readLock().lock();
|
|
|
|
try {
|
|
|
|
timeout = new HashedWheelTimeout(
|
2009-01-20 05:29:58 +01:00
|
|
|
task, wheelCursor, System.nanoTime(), initialDelay);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
wheel[schedule(timeout)].add(timeout);
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
|
|
// Start the worker if necessary.
|
|
|
|
if (activeTimeouts.getAndIncrement() == 0) {
|
|
|
|
executor.execute(worker);
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
} finally {
|
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int schedule(HashedWheelTimeout timeout) {
|
|
|
|
return schedule(timeout, timeout.initialDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
int schedule(HashedWheelTimeout timeout, final long additionalDelay) {
|
|
|
|
synchronized (timeout) {
|
|
|
|
final long oldCumulativeDelay = timeout.cumulativeDelay;
|
|
|
|
final long newCumulativeDelay = oldCumulativeDelay + additionalDelay;
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
final long lastRoundDelay = newCumulativeDelay % roundDuration;
|
2009-01-19 16:05:04 +01:00
|
|
|
final long lastTickDelay = newCumulativeDelay % tickDuration;
|
2009-01-20 08:57:45 +01:00
|
|
|
final long relativeIndex =
|
|
|
|
lastRoundDelay / tickDuration + (lastTickDelay != 0? 1 : 0);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
timeout.deadline = timeout.startTime + newCumulativeDelay;
|
|
|
|
timeout.cumulativeDelay = newCumulativeDelay;
|
|
|
|
timeout.remainingRounds =
|
2009-01-20 08:57:45 +01:00
|
|
|
additionalDelay / roundDuration -
|
|
|
|
(additionalDelay % roundDuration == 0? 1:0) - timeout.slippedRounds;
|
2009-01-19 16:05:04 +01:00
|
|
|
timeout.slippedRounds = 0;
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
return timeout.stopIndex = (int) (timeout.startIndex + relativeIndex & mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isWheelEmpty() {
|
|
|
|
for (Set<HashedWheelTimeout> bucket: wheel) {
|
|
|
|
if (!bucket.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
return true;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkDelay(long delay) {
|
|
|
|
if (delay < tickDuration) {
|
|
|
|
throw new IllegalArgumentException(
|
2009-01-20 08:57:45 +01:00
|
|
|
"delay must be greater than " + tickDuration + " nanoseconds");
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class Worker implements Runnable {
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
private volatile long threadSafeStartTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
private long tick;
|
|
|
|
|
|
|
|
Worker() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts =
|
|
|
|
new ArrayList<HashedWheelTimeout>();
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
long startTime = threadSafeStartTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
tick = 1;
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
boolean continueTheLoop;
|
|
|
|
do {
|
|
|
|
startTime = waitForNextTick(startTime);
|
|
|
|
continueTheLoop = fetchExpiredTimeouts(expiredTimeouts);
|
2009-01-19 16:05:04 +01:00
|
|
|
notifyExpiredTimeouts(expiredTimeouts);
|
2009-01-20 08:57:45 +01:00
|
|
|
} while (continueTheLoop && !ExecutorUtil.isShutdown(executor));
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
private boolean fetchExpiredTimeouts(
|
2009-01-19 16:05:04 +01:00
|
|
|
List<HashedWheelTimeout> expiredTimeouts) {
|
|
|
|
|
|
|
|
// Find the expired timeouts and decrease the round counter
|
|
|
|
// if necessary. Note that we don't send the notification
|
|
|
|
// immediately to make sure the listeners are called without
|
|
|
|
// an exclusive lock.
|
|
|
|
lock.writeLock().lock();
|
|
|
|
try {
|
2009-01-20 08:57:45 +01:00
|
|
|
int oldBucketHead = wheelCursor;
|
|
|
|
int newBucketHead = oldBucketHead + 1 & mask;
|
2009-01-19 16:05:04 +01:00
|
|
|
wheelCursor = newBucketHead;
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
|
|
ReusableIterator<HashedWheelTimeout> i = iterators[oldBucketHead];
|
|
|
|
i.rewind();
|
|
|
|
fetchExpiredTimeouts(expiredTimeouts, i);
|
|
|
|
|
|
|
|
if (activeTimeouts.get() == 0) {
|
2009-01-20 09:15:07 +01:00
|
|
|
// Exit the loop - the worker will be executed again if
|
|
|
|
// there are more timeouts to expire. Please note that
|
|
|
|
// this block is protected by a write lock where all
|
|
|
|
// scheduling operations are protected by a read lock,
|
|
|
|
// which means they are mutually exclusive and there's
|
|
|
|
// no risk of race conditions (i.e. no stalled timeouts,
|
|
|
|
// no two running workers.)
|
2009-01-20 08:57:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue the loop.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchExpiredTimeouts(
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts,
|
|
|
|
Iterator<HashedWheelTimeout> i) {
|
|
|
|
|
|
|
|
long currentTime = System.nanoTime();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
HashedWheelTimeout timeout = i.next();
|
|
|
|
synchronized (timeout) {
|
|
|
|
if (timeout.remainingRounds <= 0) {
|
|
|
|
if (timeout.deadline <= currentTime) {
|
|
|
|
i.remove();
|
|
|
|
expiredTimeouts.add(timeout);
|
|
|
|
activeTimeouts.getAndDecrement();
|
2009-01-19 16:05:04 +01:00
|
|
|
} else {
|
2009-01-20 08:57:45 +01:00
|
|
|
// A rare case where a timeout is put for the next
|
|
|
|
// round: just wait for the next round.
|
|
|
|
timeout.slippedRounds ++;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
} else {
|
|
|
|
timeout.remainingRounds --;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyExpiredTimeouts(
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts) {
|
|
|
|
// Notify the expired timeouts.
|
|
|
|
for (int i = expiredTimeouts.size() - 1; i >= 0; i --) {
|
|
|
|
expiredTimeouts.get(i).expire();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the temporary list.
|
|
|
|
expiredTimeouts.clear();
|
|
|
|
}
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
private long waitForNextTick(long startTime) {
|
2009-01-19 16:05:04 +01:00
|
|
|
for (;;) {
|
|
|
|
final long currentTime = System.nanoTime();
|
|
|
|
final long sleepTime = tickDuration * tick - (currentTime - startTime);
|
|
|
|
|
|
|
|
if (sleepTime <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(sleepTime / 1000000, (int) (sleepTime % 1000000));
|
|
|
|
} catch (InterruptedException e) {
|
2009-01-20 08:57:45 +01:00
|
|
|
if (ExecutorUtil.isShutdown(executor) || isWheelEmpty()) {
|
|
|
|
return startTime;
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the tick if overflow is expected.
|
|
|
|
if (tickDuration * tick > Long.MAX_VALUE - tickDuration) {
|
|
|
|
startTime = System.nanoTime();
|
|
|
|
tick = 1;
|
|
|
|
} else {
|
|
|
|
// Increase the tick if overflow is not likely to happen.
|
|
|
|
tick ++;
|
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
|
|
return startTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class HashedWheelTimeout implements Timeout {
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
private final TimerTask task;
|
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
final int startIndex;
|
|
|
|
int stopIndex;
|
|
|
|
|
|
|
|
final long startTime;
|
2009-01-20 05:35:33 +01:00
|
|
|
volatile long deadline;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
final long initialDelay;
|
|
|
|
long cumulativeDelay;
|
|
|
|
|
|
|
|
long remainingRounds;
|
|
|
|
long slippedRounds;
|
|
|
|
|
|
|
|
private volatile int extensionCount;
|
2009-01-20 05:29:58 +01:00
|
|
|
private volatile boolean cancelled;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
HashedWheelTimeout(TimerTask task, int startIndex, long startTime, long initialDelay) {
|
|
|
|
this.task = task;
|
2009-01-19 16:05:04 +01:00
|
|
|
this.startIndex = startIndex;
|
|
|
|
this.startTime = startTime;
|
|
|
|
this.initialDelay = initialDelay;
|
|
|
|
}
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
public TimerTask getTask() {
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
if (cancelled) {
|
2009-01-19 16:05:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-01-20 05:29:58 +01:00
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
boolean removed;
|
2009-01-20 05:29:58 +01:00
|
|
|
synchronized (this) {
|
2009-01-20 08:57:45 +01:00
|
|
|
removed = wheel[stopIndex].remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
activeTimeouts.getAndDecrement();
|
2009-01-20 05:29:58 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void extend() {
|
|
|
|
extend(initialDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void extend(long additionalDelay, TimeUnit unit) {
|
|
|
|
extend(unit.toNanos(additionalDelay));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void extend(long additionalDelay) {
|
|
|
|
checkDelay(additionalDelay);
|
2009-01-20 05:29:58 +01:00
|
|
|
if (cancelled) {
|
|
|
|
throw new IllegalStateException("cancelled");
|
|
|
|
}
|
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
lock.readLock().lock();
|
|
|
|
try {
|
2009-01-20 08:57:45 +01:00
|
|
|
// Reinsert the timeout to the appropriate bucket.
|
2009-01-19 16:05:04 +01:00
|
|
|
int newStopIndex;
|
|
|
|
synchronized (this) {
|
|
|
|
newStopIndex = stopIndex = schedule(this, additionalDelay);
|
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
|
|
if (wheel[newStopIndex].add(this)) {
|
|
|
|
|
|
|
|
// Start the worker if necessary.
|
|
|
|
if (activeTimeouts.getAndIncrement() == 0) {
|
|
|
|
executor.execute(worker);
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
} finally {
|
2009-01-20 05:29:58 +01:00
|
|
|
extensionCount ++;
|
2009-01-19 16:05:04 +01:00
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
public int getExtensionCount() {
|
2009-01-19 16:05:04 +01:00
|
|
|
return extensionCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCancelled() {
|
2009-01-20 05:29:58 +01:00
|
|
|
return cancelled;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isExpired() {
|
2009-01-20 05:35:33 +01:00
|
|
|
return cancelled || System.nanoTime() > deadline;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void expire() {
|
2009-01-20 05:29:58 +01:00
|
|
|
if (cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
try {
|
|
|
|
task.run(this);
|
|
|
|
} catch (Throwable t) {
|
2009-01-20 05:39:11 +01:00
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by " +
|
|
|
|
TimerTask.class.getSimpleName() + ".", t);
|
2009-01-20 05:29:58 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2009-01-20 05:35:33 +01:00
|
|
|
long currentTime = System.nanoTime();
|
|
|
|
long age = currentTime - startTime;
|
|
|
|
long remaining = deadline - currentTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:35:33 +01:00
|
|
|
StringBuilder buf = new StringBuilder(192);
|
|
|
|
buf.append(getClass().getSimpleName());
|
|
|
|
buf.append('(');
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append("initialDelay: ");
|
|
|
|
buf.append(initialDelay / 1000000);
|
|
|
|
buf.append(" ms, ");
|
|
|
|
|
|
|
|
buf.append("cumulativeDelay: ");
|
|
|
|
buf.append(cumulativeDelay / 1000000);
|
|
|
|
buf.append(" ms, ");
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:35:33 +01:00
|
|
|
buf.append("started: ");
|
|
|
|
buf.append(age / 1000000);
|
|
|
|
buf.append(" ms ago, ");
|
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
buf.append("deadline: ");
|
|
|
|
if (remaining > 0) {
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append(remaining / 1000000);
|
|
|
|
buf.append(" ms later, ");
|
2009-01-19 16:05:04 +01:00
|
|
|
} else if (remaining < 0) {
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append(-remaining / 1000000);
|
|
|
|
buf.append(" ms ago, ");
|
2009-01-19 16:05:04 +01:00
|
|
|
} else {
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append("now, ");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.append("extended: ");
|
|
|
|
switch (getExtensionCount()) {
|
|
|
|
case 0:
|
|
|
|
buf.append("never");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
buf.append("once");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf.append(getExtensionCount());
|
|
|
|
buf.append(" times");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCancelled()) {
|
|
|
|
buf.append (", cancelled");
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.append(')').toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|