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.

WuerfelModel.java 2.1KB

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 wuerfel.model;
  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. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. *
  15. * @author leo
  16. */
  17. public class WuerfelModel implements Runnable
  18. {
  19. private int wert;
  20. private volatile boolean laufend;
  21. private SubmissionPublisher<Integer> iPublisher;
  22. private ExecutorService eService;
  23. public WuerfelModel()
  24. {
  25. wert = 1;
  26. laufend = false;
  27. iPublisher = new SubmissionPublisher<>();
  28. eService = Executors.newSingleThreadExecutor(); // ein einziger nebenläufiger Thread
  29. }
  30. public void addWertSubscription(Subscriber<Integer> subscriber)
  31. {
  32. iPublisher.subscribe(subscriber);
  33. }
  34. public void start()
  35. {
  36. laufend = true;
  37. eService.submit(this); // Thread starten
  38. notifyAll();
  39. }
  40. public void stop()
  41. {
  42. laufend = false;
  43. }
  44. @Override
  45. public void run() {
  46. while (laufend)
  47. //while (true)
  48. {
  49. try
  50. {
  51. wert++;
  52. if (wert > 6)
  53. {
  54. wert = 1;
  55. }
  56. Thread.sleep(100);
  57. }
  58. catch (Exception e)
  59. {
  60. System.err.println(e);
  61. }
  62. //if (laufend)
  63. iPublisher.submit(wert);
  64. }
  65. while (!laufend)
  66. {
  67. try {
  68. wait();
  69. } catch (InterruptedException ex) {
  70. Logger.getLogger(WuerfelModel.class.getName()).log(Level.SEVERE, null, ex);
  71. }
  72. }
  73. }
  74. }