class AtomicFSM[S] extends AtomicReference[S]
A class for a finite state machine whose state may be transitioned atomically by any number of concurrent threads.
During the time it takes to compute a new state for the FSM, another thread may change the FSMs state, invalidating the newly computed state. To deal with this issue, AtomicFSM stores the state of the FSM using AtomicReference and after computing a new state, uses AtomicReference CAS to atomically compare the current state to the one originally used to compute the new state. If the current state does not match the original state used to compute the new state, then CAS update fails and the new state is discarded. The whole process is repeated using the updated current state. This will repeat until a new computed state successfully changes the current state. Because states may be discarded and transition functions invoked multiple times during this process, states and transition functions should never create side effects themselves. Instead all transition side effects should be placed in the onTransition function. When a state is successfully transitioned, onTransition is called with both the original and new state. This method can be used to safely create state transition side effects.
- S
the type of state
- Alphabetic
- By Inheritance
- AtomicFSM
- AtomicReference
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
AtomicFSM(s0: S)
- s0
the initial state
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
accumulateAndGet(arg0: S, arg1: BinaryOperator[S]): S
- Definition Classes
- AtomicReference
-
def
apply(transition: PartialFunction[S, S], onTransition: PartialFunction[(S, S), Unit] = PartialFunction.empty): S
Atomically transition the state of the FSM.
Atomically transition the state of the FSM. After successfully transitioning to a new state, calls the onTransition method with the previous state and the new state exactly once. Note: all state transition side effects should be placed in the onTransition method
- transition
partial function that accepts the current state and returns the next state
- onTransition
partial function that invokes side effects after a successful state transition
- returns
the new state
- Exceptions thrown
java.lang.IllegalArgumentException
if the transition function is undefined for some state
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
compareAndSet(arg0: S, arg1: S): Boolean
- Definition Classes
- AtomicReference
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
fold[X](transition: PartialFunction[S, (S, X)], onTransition: PartialFunction[(S, S), Unit] = PartialFunction.empty): X
Atomically transition the state of the FSM and return the value associated with that new state.
Atomically transition the state of the FSM and return the value associated with that new state. After successfully transitioning to a new state, calls the onTransition method with the previous state and the new state exactly once. Note: all state transition side effects should be placed in the onTransition method
- transition
partial function that accepts the current state and returns the next state and the return value
- onTransition
partial function that invokes side effects after a successful state transition
- returns
the value associated with the new state
- Exceptions thrown
java.lang.IllegalArgumentException
if the transition function is undefined for some state
-
final
def
get(): S
- Definition Classes
- AtomicReference
-
final
def
getAndAccumulate(arg0: S, arg1: BinaryOperator[S]): S
- Definition Classes
- AtomicReference
-
final
def
getAndSet(arg0: S): S
- Definition Classes
- AtomicReference
-
final
def
getAndUpdate(arg0: UnaryOperator[S]): S
- Definition Classes
- AtomicReference
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
lazySet(arg0: S): Unit
- Definition Classes
- AtomicReference
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
final
def
set(arg0: S): Unit
- Definition Classes
- AtomicReference
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AtomicReference → AnyRef → Any
-
final
def
updateAndGet(arg0: UnaryOperator[S]): S
- Definition Classes
- AtomicReference
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
weakCompareAndSet(arg0: S, arg1: S): Boolean
- Definition Classes
- AtomicReference