TimerWheel Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides support for managing a large number of timers represented by TimerItem instances. The timer wheel is optimized so that add/remove operations for timer items is constant time (O(1)).
public ref class TimerWheel : IDisposable
public class TimerWheel : IDisposable
type TimerWheel = class
interface IDisposable
Public Class TimerWheel
Implements IDisposable
- Inheritance
-
TimerWheel
- Implements
Remarks
The implementation leverage a single system timer to identity and select expired timer items managed. The timer wheel maintains a wheel comprising of sectors. Each sector contains timer items that belong to that sector. The timer wheel manages the current sector index which is incremented every time the system timer expires. All the timer items in the sector are examined to determine those that have expired. The expired timer items are removed from the sector and the corresponding expiry callbacks are invoked in a separate worker thread. Each timer items maintains the timespan, the sector index, the index within the sector, and a count indicating the number of times the sector needs to be hit before the timer item will expire. The sector span indicates the timer span value for the system timer. It is possible for an application to use to two timer wheel to manager both fine and coarse timer items. For managing fine time items, the sector span can be very small such as 5 seconds. For coarse timer items, the sector span can be larger such as 1 minute.
The application should choose the number of sectors and sector span so that the average number of items that can exist in a sector can be processed within the sector span time. The processing time includes the time it takes the examine all the timer items in the sector to determine the ones that expired but does not include the time it takes to invoke the expiry callback for the timer items. The processing of a sector involves rearranging all the items so that the expiry timer items are at the end of the list and then the list is truncated to remove the expired items.
Constructors
TimerWheel() |
Initializes a new instance of the TimerWheel class with the default number of sectors (512) and sector span (1 second). |
TimerWheel(Int32, TimeSpan) |
Initializes a new instance of the TimerWheel class. |
Fields
DefaultSectorCount |
The number of sectors in the timer wheel. This value should be a power of 2. |
DefaultSectorSpan |
The span for each sector. This gives the resolution for the timer wheel. |
MaxCurrentCount |
Maximum possible value for the count for a timer item. |
MaxSectors |
Maximum possible value for number of sectors. |
MaxSpanSeconds |
Maximum possible value for span seconds. |
Methods
Dispose() |
Disposes the timerwheel. The timerwheel will no longer be useful. |
Dispose(Boolean) |
Dispose timerwheel. |
Finalize() |
Releases resources held by the TimerWheel object before garbage collection. |