@Test public void testTimerOverflowWheelLength() throws InterruptedException { final HashedWheelTimer timer = new HashedWheelTimer( Executors.defaultThreadFactory(), 100, TimeUnit.MILLISECONDS, 32); final AtomicInteger counter = new AtomicInteger(); timer.newTimeout(new TimerTask() { @Override public void run(final Timeout timeout) throws Exception { counter.incrementAndGet(); timer.newTimeout(this, 1, TimeUnit.SECONDS); } }, 1, TimeUnit.SECONDS); Thread.sleep(3500); assertEquals(3, counter.get()); }
@Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { start(); if (task == null) { throw new NullPointerException("task"); } if (unit == null) { throw new NullPointerException("unit"); } long deadline = System.nanoTime() + unit.toNanos(delay) - startTime; HashedWheelTimeout timeout; lock.readLock().lock(); try { timeout = new HashedWheelTimeout(task, deadline); if (workerState.get() == WORKER_STATE_SHUTDOWN) { throw new IllegalStateException("Cannot enqueue after shutdown"); } wheel[timeout.stopIndex].add(timeout); } finally { lock.readLock().unlock(); } return timeout; }
HashedWheelTimer implements Timer { @Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { start(); if (task == null) { throw new NullPointerException("task"); } if (unit == null) { throw new NullPointerException("unit"); } long deadline = System.nanoTime() + unit.toNanos(delay) - startTime; HashedWheelTimeout timeout; lock.readLock().lock(); try { timeout = new HashedWheelTimeout(task, deadline); if (workerState.get() == WORKER_STATE_SHUTDOWN) { throw new IllegalStateException("Cannot enqueue after shutdown"); } wheel[timeout.stopIndex].add(timeout); } finally { lock.readLock().unlock(); } return timeout; } }
HashedWheelTimer implements Timer { @Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { start(); if (task == null) { throw new NullPointerException("task"); } if (unit == null) { throw new NullPointerException("unit"); } long deadline = System.nanoTime() + unit.toNanos(delay) - startTime; HashedWheelTimeout timeout; lock.readLock().lock(); try { timeout = new HashedWheelTimeout(task, deadline); if (workerState.get() == WORKER_STATE_SHUTDOWN) { throw new IllegalStateException("Cannot enqueue after shutdown"); } wheel[timeout.stopIndex].add(timeout); } finally { lock.readLock().unlock(); } return timeout; } HashedWheelTimer();  HashedWheelTimer(long tickDuration, TimeUnit unit);  HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel);  HashedWheelTimer(ThreadFactory threadFactory);  HashedWheelTimer(
            ThreadFactory threadFactory, long tickDuration, TimeUnit unit);  HashedWheelTimer(
            ThreadFactory threadFactory,
            long tickDuration, TimeUnit unit, int ticksPerWheel); }
HashedWheelTimer implements Timer { @Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { start(); if (task == null) { throw new NullPointerException("task"); } if (unit == null) { throw new NullPointerException("unit"); } long deadline = System.nanoTime() + unit.toNanos(delay) - startTime; HashedWheelTimeout timeout; lock.readLock().lock(); try { timeout = new HashedWheelTimeout(task, deadline); if (workerState.get() == WORKER_STATE_SHUTDOWN) { throw new IllegalStateException("Cannot enqueue after shutdown"); } wheel[timeout.stopIndex].add(timeout); } finally { lock.readLock().unlock(); } return timeout; } HashedWheelTimer();  HashedWheelTimer(long tickDuration, TimeUnit unit);  HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel);  HashedWheelTimer(ThreadFactory threadFactory);  HashedWheelTimer(
            ThreadFactory threadFactory, long tickDuration, TimeUnit unit);  HashedWheelTimer(
            ThreadFactory threadFactory,
            long tickDuration, TimeUnit unit, int ticksPerWheel); void start(); @Override Set<Timeout> stop(); @Override Timeout newTimeout(TimerTask task, long delay, TimeUnit unit); }
HashedWheelTimer implements Timer { @Override public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { start(); if (task == null) { throw new NullPointerException("task"); } if (unit == null) { throw new NullPointerException("unit"); } long deadline = System.nanoTime() + unit.toNanos(delay) - startTime; HashedWheelTimeout timeout; lock.readLock().lock(); try { timeout = new HashedWheelTimeout(task, deadline); if (workerState.get() == WORKER_STATE_SHUTDOWN) { throw new IllegalStateException("Cannot enqueue after shutdown"); } wheel[timeout.stopIndex].add(timeout); } finally { lock.readLock().unlock(); } return timeout; } HashedWheelTimer();  HashedWheelTimer(long tickDuration, TimeUnit unit);  HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel);  HashedWheelTimer(ThreadFactory threadFactory);  HashedWheelTimer(
            ThreadFactory threadFactory, long tickDuration, TimeUnit unit);  HashedWheelTimer(
            ThreadFactory threadFactory,
            long tickDuration, TimeUnit unit, int ticksPerWheel); void start(); @Override Set<Timeout> stop(); @Override Timeout newTimeout(TimerTask task, long delay, TimeUnit unit); static final int WORKER_STATE_INIT; static final int WORKER_STATE_STARTED; static final int WORKER_STATE_SHUTDOWN; }