12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package wuerfel.model;
-
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Flow.Subscriber;
- import java.util.concurrent.SubmissionPublisher;
- import java.util.logging.Level;
- import java.util.logging.Logger;
-
- /**
- *
- * @author leo
- */
- public class WuerfelModel implements Runnable
- {
- private int wert;
- private volatile boolean laufend;
- private SubmissionPublisher<Integer> iPublisher;
- private ExecutorService eService;
-
- public WuerfelModel()
- {
- wert = 1;
- laufend = false;
- iPublisher = new SubmissionPublisher<>();
- eService = Executors.newSingleThreadExecutor(); // ein einziger nebenläufiger Thread
- }
-
- public void addWertSubscription(Subscriber<Integer> subscriber)
- {
- iPublisher.subscribe(subscriber);
- }
-
- public void start()
- {
- laufend = true;
-
- eService.submit(this); // Thread starten
- notifyAll();
- }
-
-
- public void stop()
- {
- laufend = false;
- }
-
- @Override
- public void run() {
-
- while (laufend)
- //while (true)
- {
- try
- {
- wert++;
- if (wert > 6)
- {
- wert = 1;
- }
- Thread.sleep(100);
- }
- catch (Exception e)
- {
- System.err.println(e);
- }
-
- //if (laufend)
- iPublisher.submit(wert);
- }
-
- while (!laufend)
- {
- try {
- wait();
- } catch (InterruptedException ex) {
- Logger.getLogger(WuerfelModel.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
-
-
- }
- }
|