001package org.woehlke.simulation.cyclic.cellular.automaton.control;
002
003import org.woehlke.simulation.cyclic.cellular.automaton.model.CyclicCellularAutomaton;
004import org.woehlke.simulation.cyclic.cellular.automaton.view.WorldCanvas;
005
006/**
007 * (C) 2006 - 2013 Thomas Woehlke.
008 * http://thomas-woehlke.de/p/cyclic-cellular-automaton/
009 * @author Thomas Woehlke
010 * Date: 05.02.2006
011 * Time: 00:36:20
012 */
013public class ControllerThread extends Thread
014        implements Runnable {
015
016
017    private CyclicCellularAutomaton cyclicCellularAutomaton;
018    private WorldCanvas canvas;
019
020    private int THREAD_SLEEP_TIME = 100;
021    private Boolean goOn;
022
023    public ControllerThread(WorldCanvas canvas, CyclicCellularAutomaton cyclicCellularAutomaton) {
024        goOn = Boolean.TRUE;
025        this.canvas=canvas;
026        this.cyclicCellularAutomaton=cyclicCellularAutomaton;
027    }
028
029    public void run() {
030        boolean doIt;
031        do {
032            synchronized (goOn) {
033                doIt = goOn.booleanValue();
034            }
035            cyclicCellularAutomaton.step();
036            canvas.repaint();
037            try { sleep(THREAD_SLEEP_TIME); }
038            catch (InterruptedException e) { e.printStackTrace(); }
039        }
040        while (doIt);
041    }
042
043    public void exit() {
044        synchronized (goOn) {
045            goOn = Boolean.FALSE;
046        }
047    }
048
049
050}