HashedWheelTimer

cc.otavia.core.timer.HashedWheelTimer
See theHashedWheelTimer companion object
class HashedWheelTimer(val system: ActorSystem, threadFactory: ThreadFactory, val tickDuration: Long, unit: TimeUnit, val ticksPerWheel: Int, leakDetection: Boolean, maxPendingTimeouts: Long, taskExecutor: Executor) extends AtomicInteger, InternalTimer

A InternalTimer optimized for approximated I/O timeout scheduling. ===Tick Duration=== As described with 'approximated', this timer does not execute the scheduled TimerTask on time. HashedWheelTimer, on every tick, will check if there are any TimerTasks 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 of TimerTasks 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 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.

Value parameters

leakDetection

true if 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 to newTimeout will result in java.util.concurrent.RejectedExecutionException being thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.

system

ActorSystem of the HashedWheelTimer belong.

taskExecutor

The Executor that is used to execute the submitted TimerTasks. The caller is responsible to shutdown the Executor once it is not needed anymore.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

tickDuration

the duration between tick

ticksPerWheel

the size of the wheel

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

Constructor

Creates a new HashedWheelTimer.

Companion
object
Graph
Supertypes
class AtomicInteger
class Number
trait Serializable
class Object
trait Matchable
class Any
Show all

Members list

Value members

Constructors

def this(system: ActorSystem, threadFactory: ThreadFactory, tickDuration: Long, unit: TimeUnit, ticksPerWheel: Int, leakDetection: Boolean, maxPendingTimeouts: Long)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

leakDetection

true if 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 to newTimeout will result in java.util.concurrent.RejectedExecutionException being thrown. No maximum pending timeouts limit is assumed if this value is 0 or negative.

system

ActorSystem of the HashedWheelTimer belong.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

tickDuration

the duration between tick

ticksPerWheel

the size of the wheel

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, threadFactory: ThreadFactory, tickDuration: Long, unit: TimeUnit, ticksPerWheel: Int, leakDetection: Boolean)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

leakDetection

true if leak detection should be enabled always, if false it will only be enabled if the worker thread is not a daemon thread.

system

ActorSystem of the HashedWheelTimer belong.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

tickDuration

the duration between tick

ticksPerWheel

the size of the wheel

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, threadFactory: ThreadFactory, tickDuration: Long, unit: TimeUnit, ticksPerWheel: Int)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

tickDuration

the duration between tick

ticksPerWheel

the size of the wheel

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, threadFactory: ThreadFactory, tickDuration: Long, unit: TimeUnit)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

tickDuration

the duration between tick

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, threadFactory: ThreadFactory)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

threadFactory

a ThreadFactory that creates a background Thread which is dedicated to TimerTask execution.

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, tickDuration: Long, unit: TimeUnit, ticksPerWheel: Int)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

tickDuration

the duration between tick

ticksPerWheel

the size of the wheel

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem, tickDuration: Long, unit: TimeUnit)

Creates a new HashedWheelTimer.

Creates a new HashedWheelTimer.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

tickDuration

the duration between tick

unit

the time unit of the tickDuration

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

def this(system: ActorSystem)

Creates a new HashedWheelTimer with the default thread factory Executors.defaultThreadFactory(), default tick duration, and default number of ticks per wheel.

Creates a new HashedWheelTimer with the default thread factory Executors.defaultThreadFactory(), default tick duration, and default number of ticks per wheel.

Value parameters

system

ActorSystem of the HashedWheelTimer belong.

Attributes

Throws
IllegalArgumentException

if either of tickDuration and ticksPerWheel is <= 0

NullPointerException

if either of threadFactory and unit is null

Concrete methods

override def newTimeout(task: TimerTask, delay: Long, unit: TimeUnit): Timeout

Schedules the specified TimerTask for one-time execution after the specified delay.

Schedules the specified TimerTask for one-time execution after the specified delay.

Value parameters

delay

one-time execution delay.

task

task to execute.

unit

one-time execution delay time unit.

Attributes

Returns

a handle which is associated with the specified task

Throws
IllegalStateException

if this timer has been stop stopped already

RejectedExecutionException

if the pending timeouts are too many and creating new timeout can cause instability in the system.

Definition Classes
override def newTimeout(task: TimerTask, delay: Long, unit: TimeUnit, period: Long, punit: TimeUnit): Timeout

Schedules the specified TimerTask for one-time execution after the specified delay and then periodic execution with period delay.

Schedules the specified TimerTask for one-time execution after the specified delay and then periodic execution with period delay.

Value parameters

delay

one-time execution delay.

period

periodic execution delay.

punit

periodic execution delay time unit.

task

task to execute.

unit

one-time execution delay time unit.

Attributes

Returns

a handle which is associated with the specified task

Throws
IllegalStateException

if this timer has been stop stopped already

RejectedExecutionException

if the pending timeouts are too many and creating new timeout can cause instability in the system.

Definition Classes
final def start(): Unit

Starts the background thread explicitly. The background thread will start automatically on demand even if you did not call this method.

Starts the background thread explicitly. The background thread will start automatically on demand even if you did not call this method.

Attributes

Throws
IllegalStateException

if this timer has been stop stopped already

override def stop: Set[Timeout]

Releases all resources acquired by this InternalTimer and cancels all tasks which were scheduled but not executed yet.

Releases all resources acquired by this InternalTimer and cancels all tasks which were scheduled but not executed yet.

Attributes

Returns

the handles associated with the tasks which were canceled by this method

Definition Classes

Inherited methods

final def accumulateAndGet(x$0: Int, x$1: IntBinaryOperator): Int

Attributes

Inherited from:
AtomicInteger
final def addAndGet(x$0: Int): Int

Attributes

Inherited from:
AtomicInteger
def byteValue(): Byte

Attributes

Inherited from:
Number
final def compareAndExchange(x$0: Int, x$1: Int): Int

Attributes

Inherited from:
AtomicInteger
final def compareAndExchangeAcquire(x$0: Int, x$1: Int): Int

Attributes

Inherited from:
AtomicInteger
final def compareAndExchangeRelease(x$0: Int, x$1: Int): Int

Attributes

Inherited from:
AtomicInteger
final def compareAndSet(x$0: Int, x$1: Int): Boolean

Attributes

Inherited from:
AtomicInteger
final def decrementAndGet(): Int

Attributes

Inherited from:
AtomicInteger
def doubleValue(): Double

Attributes

Inherited from:
AtomicInteger
def floatValue(): Float

Attributes

Inherited from:
AtomicInteger
final def get(): Int

Attributes

Inherited from:
AtomicInteger
final def getAcquire(): Int

Attributes

Inherited from:
AtomicInteger
final def getAndAccumulate(x$0: Int, x$1: IntBinaryOperator): Int

Attributes

Inherited from:
AtomicInteger
final def getAndAdd(x$0: Int): Int

Attributes

Inherited from:
AtomicInteger
final def getAndDecrement(): Int

Attributes

Inherited from:
AtomicInteger
final def getAndIncrement(): Int

Attributes

Inherited from:
AtomicInteger
final def getAndSet(x$0: Int): Int

Attributes

Inherited from:
AtomicInteger
final def getAndUpdate(x$0: IntUnaryOperator): Int

Attributes

Inherited from:
AtomicInteger
final def getOpaque(): Int

Attributes

Inherited from:
AtomicInteger
final def getPlain(): Int

Attributes

Inherited from:
AtomicInteger
final def incrementAndGet(): Int

Attributes

Inherited from:
AtomicInteger
def intValue(): Int

Attributes

Inherited from:
AtomicInteger
final def lazySet(x$0: Int): Unit

Attributes

Inherited from:
AtomicInteger
def longValue(): Long

Attributes

Inherited from:
AtomicInteger
final def set(x$0: Int): Unit

Attributes

Inherited from:
AtomicInteger
final def setOpaque(x$0: Int): Unit

Attributes

Inherited from:
AtomicInteger
final def setPlain(x$0: Int): Unit

Attributes

Inherited from:
AtomicInteger
final def setRelease(x$0: Int): Unit

Attributes

Inherited from:
AtomicInteger
def shortValue(): Short

Attributes

Inherited from:
Number
def toString(): String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Inherited from:
AtomicInteger
final def updateAndGet(x$0: IntUnaryOperator): Int

Attributes

Inherited from:
AtomicInteger
final def weakCompareAndSetAcquire(x$0: Int, x$1: Int): Boolean

Attributes

Inherited from:
AtomicInteger
final def weakCompareAndSetPlain(x$0: Int, x$1: Int): Boolean

Attributes

Inherited from:
AtomicInteger
final def weakCompareAndSetRelease(x$0: Int, x$1: Int): Boolean

Attributes

Inherited from:
AtomicInteger
final def weakCompareAndSetVolatile(x$0: Int, x$1: Int): Boolean

Attributes

Inherited from:
AtomicInteger

Deprecated and Inherited methods

@Deprecated(since = "9")
final def weakCompareAndSet(x$0: Int, x$1: Int): Boolean

Attributes

Deprecated
true
Inherited from:
AtomicInteger

Concrete fields

val duration: Long
val tickDuration: Long
val ticksPerWheel: Int