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.

Datenmodell2.java 1.4KB

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