Skip to main content

New Article: Finite State Machine Supporting Concurrent States

Posted by editor on September 9, 2009 at 11:55 PM PDT

We've just published Jeff Lowery's new article on developing finite state machines in Java. Jeff's article demonstrates how Java enums and EnumSets can be applied to define and validate states and state transitions.

Finite state machines (FSMs) are finding increasing application today. This type of modeling reminds me of my education in physics and chemistry, where a set of distinct potential states exist, and the physical universe toggles among those possibilities. The notion of "in between" doesn't really exist in such a universe. Yet, when you think of electrons jumping between energy states, or quantum mechanics -- you realize that the basis of much of our understanding of the physical universe relies on conceiving the micro-universe as being a kind of finite state machine.

Hence, the growing recognition of the validity of applying states and state transitions to a wide variety of practical problems does indeed make a lot of sense. The macro-scale universe reflects the finite state machine nature that appears to model the micro-scale universe well.

Jeff's article starts out with describing how a typical application can be understood as consisting of a set of states and transitions between those states:

A typical application will go through multiple states during a runtime session, e.g., RUNNING -> PAUSED -> RESTARTED, or (in the case of a data entry form) ENTER -> VALIDATE -> SAVE. The state transitions are initiated by runtime events, such as when a user presses an PAUSE button or hits the ENTER key after entering data in a field. The FSM determines what actions occur when an event is received, and the resulting state. The transition to a new state will invoke a change in the immediate or subsequent behavior of an application.

Jeff goes on to further illustrate and demonstrate the principals of finite state machines and their applicability within applications through a "Bouncing Bomb" application. After describing how the code works for an initial application, Jeff goes on to describe how the application can be transformed to permit concurrent states:

Here's a question: When the application is still running, why not allow the user to set a configuration for the next run? The new configuration would then be ready for the next run when the current run ended. To enable that behavior, two states would have to be allowed to be active at once: RUNNING and CONFIGURING.

How might such concurrent states be handled? The first thing to change is the type of the currentState instance in the StateMachine. One approach is to use an EnumSet to hold both the primary and concurrent state enum values, and change the stateMap key to an EnumSet<State> and its values to an array of EnumSets.

Jeff Lowery's A Finite State Machine Supporting Concurrent States is an article I found interesting both from the point of view of the coding itself and the broader theoretical realm that the demonstration application addresses. Take a look and see what you think!

In Java Today, Stephen Colebourne provides some JDK 7 - Method suggestions:

There is lots of prior art in the field of general utility method libraries for Java.
Perhaps the best known is Apache Commons Lang.
Lets choose some methods (other than string utilities) that might be appropriate for the JDK from

1) Null-safe equals check...

Java Champion Dr. Heinz M. Kabutz talks about Creating Objects Without Calling Constructors:

A few months before disappearing amongst the sand dunes of Chania's beaches on my extended summer vacation, I was explaining to the students on my advanced Java course how deserialization worked. If the object is serializable, then it is created magically without having the constructor called. If its parent class is not serializable, then the super class no-args constructor is invoked. For example, let's begin with a superclass that does not implement Serializable ...

Adam Bien (also a Java Champion, BTW) is taking Rethinking Java EE Best Practices - On the Road this week:

I'm already looking forward to the "Real World Java EE Patterns - Rethinking Best Practices" workshop in Zurich (organized by ch-open and JUGS) (already > 50 registrations). At September 10th (Thursday) I will introduce Xtreme Lightweight Architectures (XLAs) with Java EE 6 at JUG Dresden. So see you there - and please prepare some hard, disruptive and heretical questions to ask :-).

In today's Weblogs, Osvaldo Pinali Doederlein notes that JavaFX 1.2.1 is out, fixes binding leaks and performance

Sun just released the first maintenance update for JavaFX 1.2. This release brings mostly a batch of important javafxc fixes, that I dissect in this blog...

Java programmers are used to the fact that compilation of Java source code is a relatively straightforward process, because the Java language has a simple mapping to the Java bytecode. So javac is a trivial compiler, at least in the code generation phase...

Roger Kitain continues his series with Context And Dependency Injection (JSR 299) And Servlets:

I've had questions about how to inject Web Beans into servlets and whether that is supported. In this entry I'll outline a method of accomplishing just that.

This is a simple login application that communicates to a servlet using Ajax calls from a JSP view. I'm not going to focus on the view or the protocol (Ajax) that is used to communicate with the servlet. If you are interested in that, you can check out the source (instructions at the end of this post). I'm going to focus on the servlet code and the supporting classes for the application...

Felipe Gaucho asks Long or int to declare JPA numeric primary keys?:

I have just two arguments to avoid using long as type of the primary keys of JPA Entities:

  1. Integers IDs allow tables with ~2 billion records. Yes, you read well: max(Integer) = 2,147,483,647
  2. the JPA pagination methods only accepts Integers Query setFirstResult(int startPosition) an  Query setMaxResults(int maxResult)

In the Forums, robinthapa asks How to Use keyPressed events on MeenuScreen icons and navigate throug it?: "Dear All, I am using a GridLayout(3,3) and i have icons(images) on the meenuscreen. i also have two commands, exit and select. I want to use the keycode such as UP, DOWN, LEFT RIGHT and navigate through the images and make it..."

danthedixonman has a question about General frame rates in 0.5?: "Hi all. Just wondering what sort of frame rates people are getting when there are multiple avatars? We just tried our first meeting with 6 people and frame rates were dropping down to less than 10fps when they're normally around 16fps on my..."

And demonduck finds that Java does not run on Vista 64 / IE 8: "When I first bought my Vista 64 machine, I thought that Java was supporting IE8. I thought I saw it run Java applets in IE8. Now IE8 does not run Java applets. Does anyone have a clue about if and when SUN or..."

Our current Spotlight is Terrence Barr's annoucement of 4 New Screencasts: LWUIT, JDTF, JSR 290, and JavaCard: "Our documentation team has put together four brand-new screencasts on current subjects. They are 5 minutes each in length and a great way to get introduced quickly to the highlights of each topic. I encourage you to have a look..."

The current Poll asks "What's your reaction to the JDK 7 feature list?" Today (Thursday) is the last full day of voting.

Our Feature Articles include Jeff Lowery's new article A Finite State Machine Supporting Concurrent States, which demonstrates how Java enums and EnumSets can be used as a basis to define and validate application states and state transitions. We're also featuring Jeff Friesen's article Introducing Custom Paints to JavaFX, which shows how you can leverage undocumented JavaFX capabilities to support custom paints in JavaFX Version 1.2.

The latest Java Mobility Podcast is Java Mobility Podcast 86: Mobile Service Architecture 2: Introducing New Features in Mobile Devices: "Kay Glahn from Vodafone Group R&D and Erkki Rysa from Nokia share the new features in MSA2 in this abbreviated feature from JavaOne."

Current and upcoming Java Events:

Registered users can submit event listings for the Events Page using our events submission form. All submissions go through an editorial review before being posted to the site.

Archives and Subscriptions: This blog is delivered weekdays as the Java Today RSS feed. Also, once this page is no longer featured as the front page of it will be archived along with other past issues in the Archive.

Related Topics >>