/* * 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; 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 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; } }