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.

Datenmodell1.java 1.5KB

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