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 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public Datenmodell3()
  23. {
  24. faktor = 10;
  25. wert = 1;
  26. iPublisher = new SubmissionPublisher<>();
  27. eService = Executors.newSingleThreadExecutor();
  28. laufend = true;
  29. eService.submit(this);
  30. try{
  31. wait();
  32. }
  33. catch(InterruptedException e){
  34. System.out.println(e);
  35. }
  36. }
  37. public void start()
  38. {
  39. notify();
  40. }
  41. public void stop()
  42. {
  43. try{
  44. wait();
  45. }
  46. catch(InterruptedException e){
  47. System.out.println(e);
  48. }
  49. }
  50. public void terminate(){
  51. laufend = false;
  52. }
  53. public void addWertSubscription(Subscriber<Integer> subscriber)
  54. {
  55. iPublisher.subscribe(subscriber);
  56. }
  57. @Override
  58. public void run()
  59. {
  60. while (laufend)
  61. {
  62. try
  63. {
  64. Thread.sleep(5);
  65. }
  66. catch (Exception e)
  67. {
  68. System.err.println(e);
  69. }
  70. wert = calculateWert();
  71. System.out.println(wert); // just for displaying that the thread is running
  72. iPublisher.submit(wert);
  73. }
  74. }
  75. public int calculateWert(){
  76. return (int)(Math.random()*6) + 1;
  77. }
  78. }