123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * 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<Integer> iPublisher;
- private ExecutorService eService;
- private boolean isRunning;
-
- public Datenmodell3()
- {
- faktor = 10;
- wert = 1;
- iPublisher = new SubmissionPublisher<>();
- eService = Executors.newSingleThreadExecutor();
- laufend = true;
- eService.submit(this);
- isRunning = true;
- /*
- try{
- synchronized (LOCK) {
- LOCK.wait();
- }
- }
- catch(InterruptedException e){
- System.out.println(e);
- }
- */
-
- }
-
- public synchronized void start()
- {
- isRunning = true;
- notify();
- }
-
- public synchronized void stop()
- {
- System.out.println("==============================================");
- isRunning = false;
- System.out.println("**********************************************");
- /*
- try{
- this.wait();
- System.out.println("==============================================");
- }
- catch(InterruptedException e){
- System.out.println(e);
- }
- */
- }
-
- public void terminate(){
- laufend = false;
- }
-
- public void addWertSubscription(Subscriber<Integer> subscriber)
- {
- iPublisher.subscribe(subscriber);
- }
-
- @Override
- public void run()
- {
- while (laufend)
- {
- try{
- while(isRunning)
- {
- 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);
- }
- System.out.println("-----------------");
- synchronized(this){
- wait();
- }
- System.out.println("+++++++++++++++++");
- }
- catch(InterruptedException e){
- System.out.println("Going to sleep now!");
- }
-
- }
- }
-
- public int calculateWert(){
- return (int)(Math.random()*6) + 1;
- }
- }
|