View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.cca;
2   
3   import lombok.Getter;
4   import lombok.extern.log4j.Log4j2;
5   import org.woehlke.computer.kurzweil.application.ComputerKurzweilContext;
6   import org.woehlke.computer.kurzweil.commons.tabs.TabController;
7   
8   /**
9    * Cyclic Cellular Automaton.
10   *
11   * (C) 2006 - 2013 Thomas Woehlke.
12   * http://thomas-woehlke.de/p/cyclic-cellular-automaton/
13   * @author Thomas Woehlke
14   *
15   * Date: 05.02.2006
16   * Time: 00:36:20
17   */
18  @Log4j2
19  @Getter
20  /**
21   * https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/ThreadPoolExecutor.html
22   * https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/Executor.html
23   * TODO: https://github.com/Computer-Kurzweil/computer_kurzweil/issues/18
24   * TODO: https://github.com/Computer-Kurzweil/computer_kurzweil/issues/19
25   * http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_14_004.htm
26   */
27  public class CyclicCellularAutomatonController extends Thread
28          implements TabController, CyclicCellularAutomaton {
29  
30      private static final long serialVersionUID = 3642865135701767557L;
31      private final ComputerKurzweilContext ctx;
32      private final CyclicCellularAutomatonContext tabCtx;
33      private final int threadSleepTime;
34  
35      private Boolean goOn;
36  
37      public CyclicCellularAutomatonController(
38          CyclicCellularAutomatonContext tabCtx
39      ) {
40          super(TAB_TYPE.name()+"-Controller");
41          this.tabCtx = tabCtx;
42          this.ctx = tabCtx.getCtx();
43          this.goOn = Boolean.TRUE;
44          this.threadSleepTime = this.tabCtx.getCtx().getProperties().getCca().getControl().getThreadSleepTime();
45      }
46  
47      public void run() {
48          log.info("run() - begin");
49          boolean doIt;
50          do {
51              synchronized (this.goOn) {
52                  doIt = goOn.booleanValue();
53              }
54              synchronized (this.tabCtx) {
55                  //log.info("running");
56                  this.tabCtx.getTabModel().exec();
57                  this.tabCtx.exec();
58              }
59              try { super.sleep( this.threadSleepTime ); }
60              catch (InterruptedException e) { log.info(e.getMessage()); }
61          }
62          while (doIt);
63          log.info("run() - finished");
64      }
65  
66      public void exit() {
67          log.info("exit");
68          try {
69              synchronized (goOn) {
70                  goOn = Boolean.FALSE;
71              }
72              log.info("join");
73              join();
74              log.info("joined");
75          } catch (InterruptedException e){
76              log.info(e.getMessage());
77          }
78          log.info("exited");
79      }
80  
81  }