Package org.jboss.netty.util
Class HashedWheelTimer
- java.lang.Object
-
- org.jboss.netty.util.HashedWheelTimer
-
- All Implemented Interfaces:
Timer
public class HashedWheelTimer extends java.lang.Object implements Timer
ATimer
optimized for approximated I/O timeout scheduling.Tick Duration
As described with 'approximated', this timer does not execute the scheduledTimerTask
on time.HashedWheelTimer
, on every tick, will check if there are anyTimerTask
s 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)
HashedWheelTimer
maintains a data structure called 'wheel'. To put simply, a wheel is a hash table ofTimerTask
s 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.
HashedWheelTimer
creates 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 inChannelPipelineFactory
, which results in the creation of a new thread for every connection.Implementation Details
HashedWheelTimer
is 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 class
HashedWheelTimer.HashedWheelBucket
Bucket that stores HashedWheelTimeouts.private static class
HashedWheelTimer.HashedWheelTimeout
private class
HashedWheelTimer.Worker
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.atomic.AtomicInteger
id
(package private) static InternalLogger
logger
private int
mask
private static SharedResourceMisuseDetector
misuseDetector
private long
startTime
private java.util.concurrent.CountDownLatch
startTimeInitialized
private long
tickDuration
private java.util.Queue<HashedWheelTimer.HashedWheelTimeout>
timeouts
private HashedWheelTimer.HashedWheelBucket[]
wheel
private HashedWheelTimer.Worker
worker
static int
WORKER_STATE_INIT
static int
WORKER_STATE_SHUTDOWN
static int
WORKER_STATE_STARTED
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer>
WORKER_STATE_UPDATER
private int
workerState
private java.lang.Thread
workerThread
-
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, ThreadNameDeterminer determiner, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)
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)
Timeout
newTimeout(TimerTask task, long delay, java.util.concurrent.TimeUnit unit)
Schedules the specifiedTimerTask
for one-time execution after the specified delay.private static int
normalizeTicksPerWheel(int ticksPerWheel)
void
start()
Starts the background thread explicitly.java.util.Set<Timeout>
stop()
Releases all resources acquired by thisTimer
and cancels all tasks which were scheduled but not executed yet.
-
-
-
Field Detail
-
logger
static final InternalLogger logger
-
id
private static final java.util.concurrent.atomic.AtomicInteger id
-
misuseDetector
private static final SharedResourceMisuseDetector misuseDetector
-
WORKER_STATE_UPDATER
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER
-
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
-
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
-
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 thetickDuration
ticksPerWheel
- the size of the wheel
-
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
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.
-
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
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)
Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheel
-
HashedWheelTimer
public HashedWheelTimer(java.util.concurrent.ThreadFactory threadFactory, ThreadNameDeterminer determiner, long tickDuration, java.util.concurrent.TimeUnit unit, int ticksPerWheel)
Creates a new timer.- Parameters:
threadFactory
- aThreadFactory
that creates a backgroundThread
which is dedicated toTimerTask
execution.determiner
- thread name determiner to control thread name.tickDuration
- the duration between tickunit
- the time unit of thetickDuration
ticksPerWheel
- the size of the wheel
-
-
Method Detail
-
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:Timer
Releases all resources acquired by thisTimer
and 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:Timer
Schedules the specifiedTimerTask
for one-time execution after the specified delay.- Specified by:
newTimeout
in interfaceTimer
- Returns:
- a handle which is associated with the specified task
-
-