You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Datenmodell.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package subscriber;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.Flow.Subscriber;
  10. import java.util.concurrent.SubmissionPublisher;
  11. public class Datenmodell implements Runnable
  12. {
  13. //Möglichkeit 1 bei Stopp keine Updates mehr an Anzeige
  14. private int wert;
  15. private volatile boolean laufend;
  16. private SubmissionPublisher<Integer> iPublisher;
  17. private SubmissionPublisher<Boolean> bPublisher;
  18. private ExecutorService eService;
  19. private boolean darfupdaten =true;
  20. public Datenmodell()
  21. {
  22. wert = 1;
  23. laufend = false;
  24. iPublisher = new SubmissionPublisher<>();
  25. bPublisher = new SubmissionPublisher<>();
  26. eService = Executors.newSingleThreadExecutor();
  27. }
  28. public void start()
  29. {
  30. laufend = true;
  31. darfupdaten = true;
  32. eService.submit(this);
  33. }
  34. public void stop()
  35. {
  36. laufend = false;
  37. darfupdaten =false;
  38. }
  39. public void addWertSubscription(Subscriber<Integer> subscriber)
  40. {
  41. iPublisher.subscribe(subscriber);
  42. }
  43. public void addZustandSubscription(Subscriber<Boolean> subscriber)
  44. {
  45. bPublisher.subscribe(subscriber);
  46. }
  47. @Override
  48. public void run()
  49. {
  50. while (laufend)
  51. {
  52. try
  53. {
  54. Thread.sleep(50);
  55. }
  56. catch (Exception e)
  57. {
  58. System.err.println(e);
  59. }
  60. if(wert==6){
  61. wert=0;
  62. }
  63. wert++;
  64. if(darfupdaten){
  65. iPublisher.submit(wert);
  66. }
  67. }
  68. }
  69. // Möglichkeit 2 zerstören und neuerstellen des Thread-Objektes
  70. // private int wert;
  71. // private volatile boolean laufend;
  72. // private SubmissionPublisher<Integer> iPublisher;
  73. // private SubmissionPublisher<Boolean> bPublisher;
  74. // private ExecutorService eService;
  75. //
  76. // public Datenmodell()
  77. // {
  78. // wert = 1;
  79. // laufend = false;
  80. // iPublisher = new SubmissionPublisher<>();
  81. // bPublisher = new SubmissionPublisher<>();
  82. //
  83. // }
  84. //
  85. // public void start()
  86. // {
  87. // laufend = true;
  88. // eService = Executors.newSingleThreadExecutor();
  89. // eService.submit(this);
  90. // }
  91. //
  92. // public void stop()
  93. // {
  94. // laufend = false;
  95. // eService = null;
  96. // }
  97. //
  98. // public void addWertSubscription(Subscriber<Integer> subscriber)
  99. // {
  100. // iPublisher.subscribe(subscriber);
  101. // }
  102. //
  103. // public void addZustandSubscription(Subscriber<Boolean> subscriber)
  104. // {
  105. // bPublisher.subscribe(subscriber);
  106. // }
  107. //
  108. // @Override
  109. // public void run()
  110. // {
  111. // while (laufend)
  112. // {
  113. // try
  114. // {
  115. // Thread.sleep(50);
  116. // }
  117. // catch (Exception e)
  118. // {
  119. // System.err.println(e);
  120. // }
  121. // if(wert==6){
  122. // wert=0;
  123. // }
  124. // wert++;
  125. // iPublisher.submit(wert);
  126. //
  127. //
  128. // }
  129. // }
  130. //Ende Möglichkeit 2
  131. // Möglichkeit 3 wait und notify
  132. // private int wert;
  133. // private volatile boolean await;
  134. // private SubmissionPublisher<Integer> iPublisher;
  135. // private SubmissionPublisher<Boolean> bPublisher;
  136. // private ExecutorService eService;
  137. //
  138. // public Datenmodell()
  139. // {
  140. // wert = 1;
  141. // await = false;
  142. // iPublisher = new SubmissionPublisher<>();
  143. // bPublisher = new SubmissionPublisher<>();
  144. // eService = Executors.newSingleThreadExecutor();
  145. // }
  146. //
  147. // public synchronized void start()
  148. // {
  149. // if(await){
  150. // eService.notify();
  151. // }
  152. // eService.submit(this);
  153. // }
  154. //
  155. // public synchronized void stop()
  156. // {
  157. // try{
  158. // eService.wait();
  159. //
  160. // await = true;
  161. // }catch(Exception ex){
  162. // System.out.println(ex);
  163. // }
  164. // }
  165. //
  166. // public void addWertSubscription(Subscriber<Integer> subscriber)
  167. // {
  168. // iPublisher.subscribe(subscriber);
  169. // }
  170. //
  171. // public void addZustandSubscription(Subscriber<Boolean> subscriber)
  172. // {
  173. // bPublisher.subscribe(subscriber);
  174. // }
  175. //
  176. // @Override
  177. // public void run()
  178. // {
  179. // while(true){
  180. // try
  181. // {
  182. // Thread.sleep(50);
  183. // }
  184. // catch (Exception e)
  185. // {
  186. // System.err.println(e);
  187. // }
  188. // if(wert==6){
  189. // wert=0;
  190. // }
  191. // wert++;
  192. // iPublisher.submit(wert);
  193. //
  194. // }
  195. // }
  196. }