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