Home / Class/ DefaultMockTicker Class — netty Architecture

DefaultMockTicker Class — netty Architecture

Architecture documentation for the DefaultMockTicker class in DefaultMockTicker.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  f544a2b2_a145_f856_fb22_69a427bff1ce["DefaultMockTicker"]
  5b01cb31_90fb_870b_6979_4749efe4f629["DefaultMockTicker.java"]
  f544a2b2_a145_f856_fb22_69a427bff1ce -->|defined in| 5b01cb31_90fb_870b_6979_4749efe4f629
  fef2186d_597b_bf6f_c80a_9284863e05ee["nanoTime()"]
  f544a2b2_a145_f856_fb22_69a427bff1ce -->|method| fef2186d_597b_bf6f_c80a_9284863e05ee
  a758e7d3_3755_e7fd_392c_5107c5788ed7["sleep()"]
  f544a2b2_a145_f856_fb22_69a427bff1ce -->|method| a758e7d3_3755_e7fd_392c_5107c5788ed7
  fd2c5c02_baa1_5260_e9fe_0b2674a83441["awaitSleepingThread()"]
  f544a2b2_a145_f856_fb22_69a427bff1ce -->|method| fd2c5c02_baa1_5260_e9fe_0b2674a83441
  6845c940_7a5a_9761_ec05_a25c0d9cea4d["advance()"]
  f544a2b2_a145_f856_fb22_69a427bff1ce -->|method| 6845c940_7a5a_9761_ec05_a25c0d9cea4d

Relationship Graph

Source Code

common/src/main/java/io/netty/util/concurrent/DefaultMockTicker.java lines 32–102

final class DefaultMockTicker implements MockTicker {

    // The lock is fair, so waiters get to process condition signals in the order they (the waiters) queued up.
    private final ReentrantLock lock = new ReentrantLock(true);
    private final Condition tickCondition = lock.newCondition();
    private final Condition sleeperCondition = lock.newCondition();
    private final AtomicLong nanoTime = new AtomicLong();
    private final Set<Thread> sleepers = Collections.newSetFromMap(new IdentityHashMap<>());

    @Override
    public long nanoTime() {
        return nanoTime.get();
    }

    @Override
    public void sleep(long delay, TimeUnit unit) throws InterruptedException {
        checkPositiveOrZero(delay, "delay");
        requireNonNull(unit, "unit");

        if (delay == 0) {
            return;
        }

        final long delayNanos = unit.toNanos(delay);
        lock.lockInterruptibly();
        try {
            final long startTimeNanos = nanoTime();
            sleepers.add(Thread.currentThread());
            sleeperCondition.signalAll();
            do {
                tickCondition.await();
            } while (nanoTime() - startTimeNanos < delayNanos);
        } finally {
            sleepers.remove(Thread.currentThread());
            lock.unlock();
        }
    }

    /**
     * Wait for the given thread to enter the {@link #sleep(long, TimeUnit)} method, and block.
     */
    public void awaitSleepingThread(Thread thread) throws InterruptedException {
        lock.lockInterruptibly();
        try {
            while (!sleepers.contains(thread)) {
                sleeperCondition.await();
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void advance(long amount, TimeUnit unit) {
        checkPositiveOrZero(amount, "amount");
        requireNonNull(unit, "unit");

        if (amount == 0) {
            return;
        }

        final long amountNanos = unit.toNanos(amount);
        lock.lock();
        try {
            nanoTime.addAndGet(amountNanos);
            tickCondition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

Frequently Asked Questions

What is the DefaultMockTicker class?
DefaultMockTicker is a class in the netty codebase, defined in common/src/main/java/io/netty/util/concurrent/DefaultMockTicker.java.
Where is DefaultMockTicker defined?
DefaultMockTicker is defined in common/src/main/java/io/netty/util/concurrent/DefaultMockTicker.java at line 32.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free