123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- /*
- * 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 Datenmodell1 implements Runnable
- {
- private int faktor;
- private int wert;
- public boolean active;
- private volatile boolean laufend;
- private SubmissionPublisher<Integer> iPublisher;
- private ExecutorService eService;
-
- public Datenmodell1()
- {
- faktor = 10;
- wert = 1;
- laufend = false;
- iPublisher = new SubmissionPublisher<>();
- eService = Executors.newSingleThreadExecutor();
- laufend = true;
- eService.submit(this);
-
- active = false;
- }
-
- public void start(){
- active = true;
- }
-
- public void stop()
- {
- active = false;
- }
-
- public void addWertSubscription(Subscriber<Integer> 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
- if (active){
- iPublisher.submit(wert);
- }
- }
- }
-
- public int calculateWert(){
- return (int)(Math.random()*6) + 1;
- }
- }
|