代码重构----使用java有限状态机来消除太多的if else判断

1. 状态机基本概念

http://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA

状态存储关于过去的信息,就是说:它反映从系统开始到现在时刻的输入变化。转移指示状态变更,并且用必须满足来确使转移发生的条件来描述它。动作是在给定时刻要进行的活动的描述。有多种类型的动作:

进入动作(entry action):在进入状态时进行
退出动作:在退出状态时进行
输入动作:依赖于当前状态和输入条件进行
转移动作:在进行特定转移时进行

FSM(有限状态机)可以使用上面图 1 那样的状态图(或状态转移图)来表示。此外可以使用多种类型的状态转移表。下面展示最常见的表示:当前状态(B)和条件(Y)的组合指示出下一个状态(C)。完整的动作信息可以只使用脚注来增加。包括完整动作信息的 FSM 定义可以使用状态表

状态转移表
当前状态 →
条件 ↓
状态 A 状态 B 状态 C
条件 X
条件 Y 状态 C
条件 Z

除了建模这里介绍的反应系统之外,有限状态自动机在很多不同领域中是重要的,包括电子工程语言学计算机科学哲学生物学数学逻辑学。有限状态机是在自动机理论计算理论中研究的一类自动机。在计算机科学中,有限状态机被广泛用于建模应用行为、硬件电路系统设计、软件工程,编译器、网络协议、和计算与语言的研究。

软件应用

下列概念经常用来建造有有限状态机的软件应用:

  • 事件驱动 FSM
  • 虚拟 FSM (VFSM)
  • 基于自动机编程

 2. State machines vs threads

 

Alan Cox once said "A Computer is a state machine. Threads are for people who can‘t program state machines".

http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html

Java 6 Thread States and Life Cycle

UML Protocol State Machine Diagram Example

This is an example of UML protocol state machine diagram showing thread states and thread life cycle for the Thread class in Java 6.

Thread is a lightweight process, the smallest unit of scheduled execution. Instance of the Thread class in Java 6 could be in one of the following states:

  • new,
  • runnable,
  • timed waiting,
  • waiting,
  • blocked,
  • terminated.

These states are Java Virtual Machine (JVM) states reported by JVM to Java programs. At any given point in time thread could be in only one state.

代码重构----使用java有限状态机来消除太多的if else判断

Protocol state machine example - Thread states and life cycle in Java 6

New is the thread state for a thread which was created but has not yet started.

At the lower operating system (OS) level, JVM’s runnable state could be considered as a composite state with two substates. When a thread transitions to the runnable JVM state, the thread first goes into the ready substate. Thread scheduling decides when the thread could actually start, proceed or be suspended. Thread.yield() is explicit recommendation to thread scheduler to pause the currently executing thread to allow some other thread to execute.

A thread in the runnable state is executing from the JVM point of view but in fact it may be waiting for some resources from the operating system.

Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

  • Thread.sleep(sleeptime)
  • Object.wait(timeout)
  • Thread.join(timeout)
  • LockSupport.parkNanos(timeout)
  • LockSupport.parkUntil(timeout)

A thread is in the waiting state due to the calling one of the following methods without timeout:

  • Object.wait()
  • Thread.join()
  • LockSupport.park()

Note, that thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate. It means that waiting state could be made a composite state with states corresponding to these specific conditions.

Thread is in the blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().

A synchronized statement or method acquires a mutual-exclusion lock on behalf of the executing thread, executes a block or method, and then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock and is blocked waiting for the lock.

After thread has completed execution of run() method, it is moved into terminated state.

 

3. 框架

http://*.com/questions/10875317/recommended-fsm-finite-state-machine-library-for-java

From original question:

Updates:

Also:

代码重构----使用java有限状态机来消除太多的if else判断

上一篇:Path Sum


下一篇:????Storage Multipath Subsystem Device Driver 安装配置及说明