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.

Datenmodell3.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /**
  12. *
  13. * @author le
  14. */
  15. public class Datenmodell3 implements Runnable
  16. {
  17. private int faktor;
  18. private int wert;
  19. private volatile boolean laufend;
  20. private SubmissionPublisher<Integer> iPublisher;
  21. private ExecutorService eService;
  22. private boolean isRunning;
  23. public Datenmodell3()
  24. {
  25. faktor = 10;
  26. wert = 1;
  27. iPublisher = new SubmissionPublisher<>();
  28. eService = Executors.newSingleThreadExecutor();
  29. laufend = true;
  30. eService.submit(this);
  31. isRunning = true;
  32. /*
  33. try{
  34. synchronized (LOCK) {
  35. LOCK.wait();
  36. }
  37. }
  38. catch(InterruptedException e){
  39. System.out.println(e);
  40. }
  41. */
  42. }
  43. public synchronized void start()
  44. {
  45. isRunning = true;
  46. notify();
  47. }
  48. public synchronized void stop()
  49. {
  50. System.out.println("==============================================");
  51. isRunning = false;
  52. System.out.println("**********************************************");
  53. /*
  54. try{
  55. this.wait();
  56. System.out.println("==============================================");
  57. }
  58. catch(InterruptedException e){
  59. System.out.println(e);
  60. }
  61. */
  62. }
  63. public void terminate(){
  64. laufend = false;
  65. }
  66. public void addWertSubscription(Subscriber<Integer> subscriber)
  67. {
  68. iPublisher.subscribe(subscriber);
  69. }
  70. @Override
  71. public void run()
  72. {
  73. while (laufend)
  74. {
  75. try{
  76. while(isRunning)
  77. {
  78. try
  79. {
  80. Thread.sleep(5);
  81. }
  82. catch (Exception e)
  83. {
  84. System.err.println(e);
  85. }
  86. wert = calculateWert();
  87. System.out.println(wert); // just for displaying that the thread is running
  88. iPublisher.submit(wert);
  89. }
  90. System.out.println("-----------------");
  91. synchronized(this){
  92. wait();
  93. }
  94. System.out.println("+++++++++++++++++");
  95. }
  96. catch(InterruptedException e){
  97. System.out.println("Going to sleep now!");
  98. }
  99. }
  100. }
  101. public int calculateWert(){
  102. return (int)(Math.random()*6) + 1;
  103. }
  104. }