123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /*
- * 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 subscriber;
-
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Flow.Subscriber;
- import java.util.concurrent.SubmissionPublisher;
-
-
- public class Datenmodell implements Runnable
- {
-
-
- //Möglichkeit 1 bei Stopp keine Updates mehr an Anzeige
-
-
- private int wert;
- private volatile boolean laufend;
- private SubmissionPublisher<Integer> iPublisher;
- private SubmissionPublisher<Boolean> bPublisher;
- private ExecutorService eService;
- private boolean darfupdaten =true;
-
- public Datenmodell()
- {
- wert = 1;
- laufend = false;
- iPublisher = new SubmissionPublisher<>();
- bPublisher = new SubmissionPublisher<>();
- eService = Executors.newSingleThreadExecutor();
- }
-
- public void start()
- {
- laufend = true;
- darfupdaten = true;
- eService.submit(this);
- }
-
- public void stop()
- {
- laufend = false;
- darfupdaten =false;
- }
-
- public void addWertSubscription(Subscriber<Integer> subscriber)
- {
- iPublisher.subscribe(subscriber);
- }
-
- public void addZustandSubscription(Subscriber<Boolean> subscriber)
- {
- bPublisher.subscribe(subscriber);
- }
-
- @Override
- public void run()
- {
- while (laufend)
- {
- try
- {
- Thread.sleep(50);
- }
- catch (Exception e)
- {
- System.err.println(e);
- }
- if(wert==6){
- wert=0;
- }
- wert++;
- if(darfupdaten){
- iPublisher.submit(wert);
- }
-
- }
- }
-
-
-
- // Möglichkeit 2 zerstören und neuerstellen des Thread-Objektes
-
-
-
- // private int wert;
- // private volatile boolean laufend;
- // private SubmissionPublisher<Integer> iPublisher;
- // private SubmissionPublisher<Boolean> bPublisher;
- // private ExecutorService eService;
- //
- // public Datenmodell()
- // {
- // wert = 1;
- // laufend = false;
- // iPublisher = new SubmissionPublisher<>();
- // bPublisher = new SubmissionPublisher<>();
- //
- // }
- //
- // public void start()
- // {
- // laufend = true;
- // eService = Executors.newSingleThreadExecutor();
- // eService.submit(this);
- // }
- //
- // public void stop()
- // {
- // laufend = false;
- // eService = null;
- // }
- //
- // public void addWertSubscription(Subscriber<Integer> subscriber)
- // {
- // iPublisher.subscribe(subscriber);
- // }
- //
- // public void addZustandSubscription(Subscriber<Boolean> subscriber)
- // {
- // bPublisher.subscribe(subscriber);
- // }
- //
- // @Override
- // public void run()
- // {
- // while (laufend)
- // {
- // try
- // {
- // Thread.sleep(50);
- // }
- // catch (Exception e)
- // {
- // System.err.println(e);
- // }
- // if(wert==6){
- // wert=0;
- // }
- // wert++;
- // iPublisher.submit(wert);
- //
- //
- // }
- // }
-
-
- //Ende Möglichkeit 2
-
-
-
- // Möglichkeit 3 wait und notify
-
- // private int wert;
- // private volatile boolean await;
- // private SubmissionPublisher<Integer> iPublisher;
- // private SubmissionPublisher<Boolean> bPublisher;
- // private ExecutorService eService;
- //
- // public Datenmodell()
- // {
- // wert = 1;
- // await = false;
- // iPublisher = new SubmissionPublisher<>();
- // bPublisher = new SubmissionPublisher<>();
- // eService = Executors.newSingleThreadExecutor();
- // }
- //
- // public synchronized void start()
- // {
- // if(await){
- // eService.notify();
- // }
- // eService.submit(this);
- // }
- //
- // public synchronized void stop()
- // {
- // try{
- // eService.wait();
- //
- // await = true;
- // }catch(Exception ex){
- // System.out.println(ex);
- // }
- // }
- //
- // public void addWertSubscription(Subscriber<Integer> subscriber)
- // {
- // iPublisher.subscribe(subscriber);
- // }
- //
- // public void addZustandSubscription(Subscriber<Boolean> subscriber)
- // {
- // bPublisher.subscribe(subscriber);
- // }
- //
- // @Override
- // public void run()
- // {
- // while(true){
- // try
- // {
- // Thread.sleep(50);
- // }
- // catch (Exception e)
- // {
- // System.err.println(e);
- // }
- // if(wert==6){
- // wert=0;
- // }
- // wert++;
- // iPublisher.submit(wert);
- //
- // }
- // }
-
- }
-
-
|