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