Home / Class/ HashedWheelTimeout Class — netty Architecture

HashedWheelTimeout Class — netty Architecture

Architecture documentation for the HashedWheelTimeout class in HashedWheelTimer.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  8bee3e2e_d827_0301_76b0_12fc08d40d2b["HashedWheelTimeout"]
  8be6e914_922c_a17c_9e45_cdcc72002795["HashedWheelTimer.java"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|defined in| 8be6e914_922c_a17c_9e45_cdcc72002795
  00653983_4442_5cb1_c3f9_bf5c5ba28319["HashedWheelTimeout()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 00653983_4442_5cb1_c3f9_bf5c5ba28319
  2691d2e0_86ac_5f54_2a2d_fa2f5095a9f3["Timer()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 2691d2e0_86ac_5f54_2a2d_fa2f5095a9f3
  09ac2ff4_e2f4_8d0d_018c_6d5166214c7e["TimerTask()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 09ac2ff4_e2f4_8d0d_018c_6d5166214c7e
  c0829aeb_3b87_2da4_8623_2515c85ff6b5["cancel()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| c0829aeb_3b87_2da4_8623_2515c85ff6b5
  c308ba74_e543_9d57_bba1_6d9433c57e5f["remove()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| c308ba74_e543_9d57_bba1_6d9433c57e5f
  50a46f24_2569_1284_1d49_ba6191429435["removeAfterCancellation()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 50a46f24_2569_1284_1d49_ba6191429435
  5d04c7cd_738b_2713_de7a_1e3dbddb9ae8["compareAndSetState()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 5d04c7cd_738b_2713_de7a_1e3dbddb9ae8
  a5248b2c_32f7_6134_ae9f_c1399b1b98d9["state()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| a5248b2c_32f7_6134_ae9f_c1399b1b98d9
  5966b70d_fc04_1bcb_0af7_d7180add2531["isCancelled()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 5966b70d_fc04_1bcb_0af7_d7180add2531
  3388ecb7_061a_8574_0a63_73fc87f5993d["isExpired()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 3388ecb7_061a_8574_0a63_73fc87f5993d
  4f6afa39_8a70_e3f7_e83b_a31c395ffeda["expire()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 4f6afa39_8a70_e3f7_e83b_a31c395ffeda
  00b766fb_0621_6e36_49ab_b8d6c53c1de8["run()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 00b766fb_0621_6e36_49ab_b8d6c53c1de8
  71df845b_485b_20c8_ca7c_100fdf9dd225["String()"]
  8bee3e2e_d827_0301_76b0_12fc08d40d2b -->|method| 71df845b_485b_20c8_ca7c_100fdf9dd225

Relationship Graph

Source Code

common/src/main/java/io/netty/util/HashedWheelTimer.java lines 612–753

    private static final class HashedWheelTimeout implements Timeout, Runnable {

        private static final int ST_INIT = 0;
        private static final int ST_CANCELLED = 1;
        private static final int ST_EXPIRED = 2;
        private static final AtomicIntegerFieldUpdater<HashedWheelTimeout> STATE_UPDATER =
                AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimeout.class, "state");

        private final HashedWheelTimer timer;
        private final TimerTask task;
        private final long deadline;

        @SuppressWarnings({"unused", "FieldMayBeFinal", "RedundantFieldInitialization" })
        private volatile int state = ST_INIT;

        // remainingRounds will be calculated and set by Worker.transferTimeoutsToBuckets() before the
        // HashedWheelTimeout will be added to the correct HashedWheelBucket.
        long remainingRounds;

        // This will be used to chain timeouts in HashedWheelTimerBucket via a double-linked-list.
        // As only the workerThread will act on it there is no need for synchronization / volatile.
        HashedWheelTimeout next;
        HashedWheelTimeout prev;

        // The bucket to which the timeout was added
        HashedWheelBucket bucket;

        HashedWheelTimeout(HashedWheelTimer timer, TimerTask task, long deadline) {
            this.timer = timer;
            this.task = task;
            this.deadline = deadline;
        }

        @Override
        public Timer timer() {
            return timer;
        }

        @Override
        public TimerTask task() {
            return task;
        }

        @Override
        public boolean cancel() {
            // only update the state it will be removed from HashedWheelBucket on next tick.
            if (!compareAndSetState(ST_INIT, ST_CANCELLED)) {
                return false;
            }
            // If a task should be canceled we put this to another queue which will be processed on each tick.
            // So this means that we will have a GC latency of max. 1 tick duration which is good enough. This way
            // we can make again use of our MpscLinkedQueue and so minimize the locking / overhead as much as possible.
            timer.cancelledTimeouts.add(this);
            return true;
        }

        private void remove() {
            HashedWheelBucket bucket = this.bucket;
            if (bucket != null) {
                bucket.remove(this);
            }
            timer.pendingTimeouts.decrementAndGet();
        }
        void removeAfterCancellation() {
            remove();
            task.cancelled(this);
        }

        public boolean compareAndSetState(int expected, int state) {
            return STATE_UPDATER.compareAndSet(this, expected, state);
        }

        public int state() {
            return state;
        }

        @Override
        public boolean isCancelled() {
            return state() == ST_CANCELLED;
        }

Frequently Asked Questions

What is the HashedWheelTimeout class?
HashedWheelTimeout is a class in the netty codebase, defined in common/src/main/java/io/netty/util/HashedWheelTimer.java.
Where is HashedWheelTimeout defined?
HashedWheelTimeout is defined in common/src/main/java/io/netty/util/HashedWheelTimer.java at line 612.

Analyze Your Own Codebase

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

Try Supermodel Free