View Javadoc
1   package org.woehlke.simulation.cyclic.cellular.automaton.control;
2   
3   import org.woehlke.simulation.cyclic.cellular.automaton.model.CyclicCellularAutomaton;
4   import org.woehlke.simulation.cyclic.cellular.automaton.view.WorldCanvas;
5   
6   /**
7    * (C) 2006 - 2013 Thomas Woehlke.
8    * http://thomas-woehlke.de/p/cyclic-cellular-automaton/
9    * @author Thomas Woehlke
10   * Date: 05.02.2006
11   * Time: 00:36:20
12   */
13  public class ControllerThread extends Thread
14          implements Runnable {
15  
16  
17      private CyclicCellularAutomaton cyclicCellularAutomaton;
18      private WorldCanvas canvas;
19  
20      private int THREAD_SLEEP_TIME = 100;
21      private Boolean goOn;
22  
23      public ControllerThread(WorldCanvas canvas, CyclicCellularAutomaton cyclicCellularAutomaton) {
24          goOn = Boolean.TRUE;
25          this.canvas=canvas;
26          this.cyclicCellularAutomaton=cyclicCellularAutomaton;
27      }
28  
29      public void run() {
30          boolean doIt;
31          do {
32              synchronized (goOn) {
33                  doIt = goOn.booleanValue();
34              }
35              cyclicCellularAutomaton.step();
36              canvas.repaint();
37              try { sleep(THREAD_SLEEP_TIME); }
38              catch (InterruptedException e) { e.printStackTrace(); }
39          }
40          while (doIt);
41      }
42  
43      public void exit() {
44          synchronized (goOn) {
45              goOn = Boolean.FALSE;
46          }
47      }
48  
49  
50  }