Package io.netty.util
Class HashedWheelTimer
- java.lang.Object
-
- io.netty.util.HashedWheelTimer
-
- All Implemented Interfaces:
Timer
public class HashedWheelTimer extends java.lang.Object implements Timer
ATimeroptimized for approximated I/O timeout scheduling.Tick Duration
As described with 'approximated', this timer does not execute the scheduledTimerTaskon time.HashedWheelTimer, on every tick, will check if there are anyTimerTasks behind the schedule and execute them.You can increase or decrease the accuracy of the execution timing by specifying smaller or larger tick duration in the constructor. In most network applications, I/O timeout does not need to be accurate. Therefore, the default tick duration is 100 milliseconds and you will not need to try different configurations in most cases.
Ticks per Wheel (Wheel Size)
HashedWheelTimermaintains a data structure called 'wheel'. To put simply, a wheel is a hash table ofTimerTasks whose hash function is 'dead line of the task'. The default number of ticks per wheel (i.e. the size of the wheel) is 512. You could specify a larger value if you are going to schedule a lot of timeouts.Do not create many instances.
HashedWheelTimercreates a new thread whenever it is instantiated and started. Therefore, you should make sure to create only one instance and share it across your application. One of the common mistakes, that makes your application unresponsive, is to create a new instance for every connection.Implementation Details
HashedWheelTimeris based on George Varghese and Tony Lauck's paper, 'Hashed and Hierarchical Timing Wheels: data structures to efficiently implement a timer facility'. More comprehensive slides are located here.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classHashedWheelTimer.HashedWheelBucketBucket that stores HashedWheelTimeouts.private static classHashedWheelTimer.HashedWheelTimeoutprivate classHashedWheelTimer.Worker
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<HashedWheelTimer.HashedWheelTimeout>cancelledTimeoutsprivate static intINSTANCE_COUNT_LIMITprivate static java.util.concurrent.atomic.AtomicIntegerINSTANCE_COUNTERprivate ResourceLeakTracker<HashedWheelTimer>leakprivate static ResourceLeakDetector<HashedWheelTimer>leakDetector(package private) static InternalLoggerloggerprivate intmaskprivate longmaxPendingTimeoutsprivate static longMILLISECOND_NANOSprivate java.util.concurrent.atomic.AtomicLongpendingTimeoutsprivate longstartTimeprivate java.util.concurrent.CountDownLatchstartTimeInitializedprivate longtickDurationprivate java.util.Queue<HashedWheelTimer.HashedWheelTimeout>timeoutsprivate static java.util.concurrent.atomic.AtomicBooleanWARNED_TOO_MANY_INSTANCESprivate HashedWheelTimer.HashedWheelBucket[]wheelprivate HashedWheelTimer.Workerworkerstatic intWORKER_STATE_INITstatic intWORKER_STATE_SHUTDOWNstatic intWORKER_STATE_STARTEDprivate static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer>WORKER_STATE_UPDATERprivate intworkerStateprivate java.lang.ThreadworkerThread
-
Constructor Summary
Constructors Constructor Description HashedWheelTimer()Creates a new timer with the default thread factory (Executors.defaultThreadFactory()), default tick duration, and default number of ticks per wheel.HashedWheelTimer(long tickDuration, java.util.concurrent.TimeUnit unit)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()) and default number of ticks per wheel.HashedWheelTimer(long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()).HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory)Creates a new timer with the default tick duration and default number of ticks per wheel.HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit)Creates a new timer with the default number of ticks per wheel.HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)Creates a new timer.HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel, boolean leakDetection)Creates a new timer.HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts)Creates a new timer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static HashedWheelTimer.HashedWheelBucket[]createWheel(int ticksPerWheel)protected voidfinalize()TimeoutnewTimeout(TimerTask task, long delay, java.util.concurrent.TimeUnit unit)Schedules the specifiedTimerTaskfor one-time execution after the specified delay.private static intnormalizeTicksPerWheel(int ticksPerWheel)longpendingTimeouts()Returns the number of pending timeouts of thisTimer.private static voidreportTooManyInstances()voidstart()Starts the background thread explicitly.java.util.Set<Timeout>stop()Releases all resources acquired by thisTimerand cancels all tasks which were scheduled but not executed yet.
-
-
-
Field Detail
-
logger
static final InternalLogger logger
-
INSTANCE_COUNTER
private static final java.util.concurrent.atomic.AtomicInteger INSTANCE_COUNTER
-
WARNED_TOO_MANY_INSTANCES
private static final java.util.concurrent.atomic.AtomicBoolean WARNED_TOO_MANY_INSTANCES
-
INSTANCE_COUNT_LIMIT
private static final int INSTANCE_COUNT_LIMIT
- See Also:
- Constant Field Values
-
MILLISECOND_NANOS
private static final long MILLISECOND_NANOS
-
leakDetector
private static final ResourceLeakDetector<HashedWheelTimer> leakDetector
-
WORKER_STATE_UPDATER
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER
-
leak
private final ResourceLeakTracker<HashedWheelTimer> leak
-
worker
private final HashedWheelTimer.Worker worker
-
workerThread
private final java.lang.Thread workerThread
-
WORKER_STATE_INIT
public static final int WORKER_STATE_INIT
- See Also:
- Constant Field Values
-
WORKER_STATE_STARTED
public static final int WORKER_STATE_STARTED
- See Also:
- Constant Field Values
-
WORKER_STATE_SHUTDOWN
public static final int WORKER_STATE_SHUTDOWN
- See Also:
- Constant Field Values
-
workerState
private volatile int workerState
-
tickDuration
private final long tickDuration
-
wheel
private final HashedWheelTimer.HashedWheelBucket[] wheel
-
mask
private final int mask
-
startTimeInitialized
private final java.util.concurrent.CountDownLatch startTimeInitialized
-
timeouts
private final java.util.Queue<HashedWheelTimer.HashedWheelTimeout> timeouts
-
cancelledTimeouts
private final java.util.Queue<HashedWheelTimer.HashedWheelTimeout> cancelledTimeouts
-
pendingTimeouts
private final java.util.concurrent.atomic.AtomicLong pendingTimeouts
-
maxPendingTimeouts
private final long maxPendingTimeouts
-
startTime
private volatile long startTime
-
-
Constructor Detail
-
HashedWheelTimer
public HashedWheelTimer()
Creates a new timer with the default thread factory (Executors.defaultThreadFactory()), default tick duration, and default number of ticks per wheel.
-
HashedWheelTimer
public HashedWheelTimer(long tickDuration, java.util.concurrent.TimeUnit unit)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()) and default number of ticks per wheel.- Parameters:
tickDuration- the duration between tickunit- the time unit of thetickDuration- Throws:
java.lang.NullPointerException- ifunitisnulljava.lang.IllegalArgumentException- iftickDurationis <= 0
-
HashedWheelTimer
public HashedWheelTimer(long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)Creates a new timer with the default thread factory (Executors.defaultThreadFactory()).- Parameters:
tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheel- Throws:
java.lang.NullPointerException- ifunitisnulljava.lang.IllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory)
Creates a new timer with the default tick duration and default number of ticks per wheel.- Parameters:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.- Throws:
java.lang.NullPointerException- ifthreadFactoryisnull
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit)Creates a new timer with the default number of ticks per wheel.- Parameters:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDuration- Throws:
java.lang.NullPointerException- if either ofthreadFactoryandunitisnulljava.lang.IllegalArgumentException- iftickDurationis <= 0
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)Creates a new timer.- Parameters:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheel- Throws:
java.lang.NullPointerException- if either ofthreadFactoryandunitisnulljava.lang.IllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel, boolean leakDetection)Creates a new timer.- Parameters:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheelleakDetection-trueif leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.- Throws:
java.lang.NullPointerException- if either ofthreadFactoryandunitisnulljava.lang.IllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel, boolean leakDetection, long maxPendingTimeouts)Creates a new timer.- Parameters:
threadFactory- aThreadFactorythat creates a backgroundThreadwhich is dedicated toTimerTaskexecution.tickDuration- the duration between tickunit- the time unit of thetickDurationticksPerWheel- the size of the wheelleakDetection-trueif leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.maxPendingTimeouts- The maximum number of pending timeouts after which call tonewTimeoutwill result inRejectedExecutionExceptionbeing thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.- Throws:
java.lang.NullPointerException- if either ofthreadFactoryandunitisnulljava.lang.IllegalArgumentException- if either oftickDurationandticksPerWheelis <= 0
-
-
Method Detail
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
createWheel
private static HashedWheelTimer.HashedWheelBucket[] createWheel(int ticksPerWheel)
-
normalizeTicksPerWheel
private static int normalizeTicksPerWheel(int ticksPerWheel)
-
start
public void start()
Starts the background thread explicitly. The background thread will start automatically on demand even if you did not call this method.- Throws:
java.lang.IllegalStateException- if this timer has been stopped already
-
stop
public java.util.Set<Timeout> stop()
Description copied from interface:TimerReleases all resources acquired by thisTimerand cancels all tasks which were scheduled but not executed yet.
-
newTimeout
public Timeout newTimeout(TimerTask task, long delay, java.util.concurrent.TimeUnit unit)
Description copied from interface:TimerSchedules the specifiedTimerTaskfor one-time execution after the specified delay.- Specified by:
newTimeoutin interfaceTimer- Returns:
- a handle which is associated with the specified task
-
pendingTimeouts
public long pendingTimeouts()
Returns the number of pending timeouts of thisTimer.
-
reportTooManyInstances
private static void reportTooManyInstances()
-
-