commit 7d90085271ca1d589b630c18a90115f37757935d Author: marku Date: Wed Nov 6 11:52:27 2019 +0100 1 - First Try diff --git a/src/subscriber/Datenmodell3.java b/src/subscriber/Datenmodell3.java new file mode 100644 index 0000000..49af048 --- /dev/null +++ b/src/subscriber/Datenmodell3.java @@ -0,0 +1,88 @@ +/* + * 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; + +/** + * + * @author le + */ +public class Datenmodell3 implements Runnable +{ + private int faktor; + private int wert; + private volatile boolean laufend; + private SubmissionPublisher iPublisher; + private ExecutorService eService; + + public Datenmodell3() + { + faktor = 10; + wert = 1; + iPublisher = new SubmissionPublisher<>(); + eService = Executors.newSingleThreadExecutor(); + laufend = true; + eService.submit(this); + try{ + wait(); + } + catch(InterruptedException e){ + System.out.println(e); + } + } + + public void start() + { + notify(); + } + + public void stop() + { + try{ + wait(); + } + catch(InterruptedException e){ + System.out.println(e); + } + } + + public void terminate(){ + laufend = false; + } + + public void addWertSubscription(Subscriber subscriber) + { + iPublisher.subscribe(subscriber); + } + + @Override + public void run() + { + while (laufend) + { + try + { + Thread.sleep(5); + } + catch (Exception e) + { + System.err.println(e); + } + wert = calculateWert(); + System.out.println(wert); // just for displaying that the thread is running + iPublisher.submit(wert); + } + } + + public int calculateWert(){ + return (int)(Math.random()*6) + 1; + } +}