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