title: Threaded State Machines in Java
body: <p>Is there a way of Holding a thread in a State waiting for changes?
  I mean wait tll something happend (change var, call method, etc..)
  perhaps it needs using Event Listeners, or synchronized objects/methods.. </p>
  
  <p>The usual aproach for statemachines like this</p>
  
  <p><a href="https://stackoverflow.com/questions/255797/uses-for-state-machines/256011#256011">statemachine example</a></p>
  
  <p>That uses a do{..}while(true) loop that could work for singled threaded(and no GUI) application but it CANT'T be used with threads.. 
  (at least you want to use a core for every threaded state machine)</p>
  
  <p>So to avoid that processor consuming task a simple (and ugly) way 
  is a "periodic checker", I mean adding a Sleep
  other idea is defining a synchronized object and using wait instead of thread Sleep</p>
  
  <p>example:</p>
  
  <pre><code>do{
      Switch(state)
      {         
          case STATE_A:
              //..A things                         
              break;
  
          case STATE_B:
              //..B things                         
              break;
  
          ...
  
          case STATE_Z:
              //..Z things                         
              break;
      }
      // Here!  =&gt;   wait()?  Thread.sleep(TIME_CONST)? //Hold and yield
  }while(powerOn);
  </code></pre>
  
  <p>The drawback is adding complexity using synchronized or that anything that happen within TIME_CONST would be invisible</p>
  
  <p>I would like to know other ideas for doing this, 
  thanks !</p>