{ | { | ||||
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | ||||
private int wert; | |||||
//private int wert; | |||||
private WuerfelData data; | |||||
private volatile boolean laufend; | private volatile boolean laufend; | ||||
private ExecutorService eService; | private ExecutorService eService; | ||||
private final Object LOCK; | private final Object LOCK; | ||||
private SubmissionPublisher<Integer> wertPublisher; | |||||
private SubmissionPublisher<WuerfelData> wertPublisher; | |||||
public Wuerfel(Zahlengenerator hauptgenerator) | |||||
public Wuerfel(int id) | |||||
{ | { | ||||
wert = 0; | |||||
data = new WuerfelData(id, 0); | |||||
laufend = false; | laufend = false; | ||||
eService = Executors.newSingleThreadExecutor(); | |||||
eService = null; | |||||
wertPublisher = new SubmissionPublisher<>(); | wertPublisher = new SubmissionPublisher<>(); | ||||
this.addWertSubscription(hauptgenerator); | |||||
LOCK = new Object(); | LOCK = new Object(); | ||||
} | } | ||||
public void addWertSubscription(Subscriber<Integer> subscriber) | |||||
public void addWertSubscription(Subscriber<WuerfelData> subscriber) | |||||
{ | { | ||||
wertPublisher.subscribe(subscriber); | wertPublisher.subscribe(subscriber); | ||||
} | } | ||||
public void start() | public void start() | ||||
{ | { | ||||
laufend = true; | |||||
synchronized (LOCK) | |||||
{ | |||||
LOCK.notifyAll(); | |||||
} | |||||
synchronized (this) { | |||||
laufend = true; | |||||
} | |||||
if (eService.isShutdown()) | |||||
if (eService == null) | |||||
{ | { | ||||
eService = Executors.newSingleThreadExecutor(); | eService = Executors.newSingleThreadExecutor(); | ||||
eService.execute(this); | |||||
} | |||||
synchronized (LOCK) | |||||
{ | |||||
LOCK.notifyAll(); | |||||
} | } | ||||
eService.execute(this); | |||||
} | } | ||||
public void stop() | public void stop() | ||||
{ | { | ||||
synchronized(LOCK){ | |||||
synchronized(this){ | |||||
laufend = false; | laufend = false; | ||||
} | } | ||||
} | } | ||||
System.err.println(ex); | System.err.println(ex); | ||||
} | } | ||||
this.berechneWert(); | this.berechneWert(); | ||||
wertPublisher.submit(wert); | |||||
// lg.info("submit " + data.getValue()); | |||||
wertPublisher.submit(data); | |||||
} | } | ||||
} | } | ||||
private synchronized void berechneWert() | private synchronized void berechneWert() | ||||
{ | { | ||||
wert = (int) (1 + 6*Math.random()); | |||||
data.setValue((int) (1 + 6*Math.random())); | |||||
} | } | ||||
} | } | ||||
/* | |||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |||||
*/ | |||||
package bandit.Model; | |||||
/** | |||||
* | |||||
* @author Js-Sc | |||||
*/ | |||||
public class WuerfelData { | |||||
private int id; | |||||
private int value; | |||||
WuerfelData(int id, int value){ | |||||
this.id = id; | |||||
this.value = value; | |||||
} | |||||
/** | |||||
* @return the id | |||||
*/ | |||||
public int getId() { | |||||
return id; | |||||
} | |||||
/** | |||||
* @return the value | |||||
*/ | |||||
public int getValue() { | |||||
return value; | |||||
} | |||||
/** | |||||
* @param value the value to set | |||||
*/ | |||||
public void setValue(int value) { | |||||
this.value = value; | |||||
} | |||||
} |
import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | ||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
import bandit.util.OhmLogger; | import bandit.util.OhmLogger; | ||||
import java.util.ArrayList; | |||||
import java.util.concurrent.Flow; | import java.util.concurrent.Flow; | ||||
/** | /** | ||||
* | * | ||||
* @author le | * @author le | ||||
*/ | */ | ||||
public class Zahlengenerator implements Subscriber<Integer> // Callable | |||||
public class Zahlengenerator implements Subscriber<WuerfelData> // Callable | |||||
{ | { | ||||
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | ||||
private Flow.Subscription subscription; | |||||
private SubmissionPublisher<Integer> wertPublisher; | |||||
private ArrayList<Flow.Subscription> subscriptions; | |||||
//private Flow.Subscription subscription; | |||||
private SubmissionPublisher<WuerfelData> wertPublisher; | |||||
private Wuerfel wuerfel_1; | |||||
private Wuerfel wuerfel_2; | |||||
private Wuerfel wuerfel_3; | |||||
private Wuerfel generator1; | |||||
private Thread thd1; | |||||
// private Generator2 generator2; | |||||
// private Thread thd2; | |||||
// | |||||
// private Generator3 generator3; | |||||
// private Thread thd3; | |||||
// | |||||
public Zahlengenerator() | public Zahlengenerator() | ||||
{ | { | ||||
subscriptions = new ArrayList<>(); | |||||
wertPublisher = new SubmissionPublisher<>(); | wertPublisher = new SubmissionPublisher<>(); | ||||
generator1 = new Wuerfel(this); | |||||
// generator2 = new Generator2(this); | |||||
// generator3 = new Generator3(this); | |||||
wuerfel_1 = new Wuerfel(1); | |||||
wuerfel_2 = new Wuerfel(2); | |||||
wuerfel_3 = new Wuerfel(3); | |||||
synchronized(this){ | |||||
wuerfel_1.addWertSubscription(this); | |||||
wuerfel_2.addWertSubscription(this); | |||||
wuerfel_3.addWertSubscription(this); | |||||
} | |||||
} | } | ||||
public void addWertSubscription(Subscriber<Integer> subscriber) | |||||
public void addWertSubscription(Subscriber<WuerfelData> subscriber) | |||||
{ | { | ||||
wertPublisher.subscribe(subscriber); | wertPublisher.subscribe(subscriber); | ||||
} | } | ||||
public void start() | public void start() | ||||
{ | { | ||||
if(thd1==null){ | |||||
thd1 = new Thread(generator1); | |||||
lg.info("thd1.start"); | |||||
thd1.start(); | |||||
} | |||||
// if(thd2==null){ | |||||
// thd2 = new Thread(generator2); | |||||
// thd2.start(); | |||||
// } | |||||
// if(thd3==null){ | |||||
// thd3 = new Thread(generator3); | |||||
// thd3.start(); | |||||
// } | |||||
lg.info("generator1.start"); | |||||
generator1.start(); | |||||
//generator2.start(); | |||||
//generator3.start(); | |||||
lg.info("Generator gestartet"); | |||||
wuerfel_1.start(); | |||||
wuerfel_2.start(); | |||||
wuerfel_3.start(); | |||||
} | } | ||||
public void stop() | public void stop() | ||||
{ | { | ||||
generator1.stop(); | |||||
//generator2.stop(); | |||||
//generator3.stop(); | |||||
lg.info("Generator gestopt"); | |||||
wuerfel_1.stop(); | |||||
wuerfel_2.stop(); | |||||
wuerfel_3.stop(); | |||||
} | } | ||||
@Override | @Override | ||||
public void onSubscribe(Flow.Subscription subscription) { | public void onSubscribe(Flow.Subscription subscription) { | ||||
this.subscription = subscription; | |||||
this.subscription.request(1); | |||||
lg.info("on subscribe"); | |||||
this.subscriptions.add(subscription); | |||||
subscription.request(1); | |||||
} | } | ||||
@Override | @Override | ||||
public void onNext(Integer item) { | |||||
public void onNext(WuerfelData item) { | |||||
lg.info("on Next"); | |||||
this.wertPublisher.submit(item); | this.wertPublisher.submit(item); | ||||
this.subscription.request(1); | |||||
this.subscriptions.get(item.getId() - 1).request(1); | |||||
} | } | ||||
@Override | @Override |
package bandit.Schnittstelle; | package bandit.Schnittstelle; | ||||
import bandit.Model.WuerfelData; | |||||
import bandit.Model.Zahlengenerator; | import bandit.Model.Zahlengenerator; | ||||
import bandit.View.ZahlenView; | import bandit.View.ZahlenView; | ||||
import java.util.concurrent.Flow; | import java.util.concurrent.Flow; | ||||
* | * | ||||
* @author le | * @author le | ||||
*/ | */ | ||||
public class WertAdapter implements Subscriber<Integer> | |||||
public class WertAdapter implements Subscriber<WuerfelData> | |||||
{ | { | ||||
private ZahlenView view; | private ZahlenView view; | ||||
private Zahlengenerator model; | private Zahlengenerator model; | ||||
} | } | ||||
@Override | @Override | ||||
public void onNext(Integer item) | |||||
public void onNext(WuerfelData item) | |||||
{ | { | ||||
String strWert = String.valueOf(item); | |||||
view.getLblZahl1().setText(strWert); | |||||
String strWert = String.valueOf(item.getValue()); | |||||
switch(item.getId()){ | |||||
case 1: | |||||
view.getLblZahl1().setText(strWert); | |||||
break; | |||||
case 2: | |||||
view.getLblZahl2().setText(strWert); | |||||
break; | |||||
case 3: | |||||
view.getLblZahl3().setText(strWert); | |||||
break; | |||||
} | |||||
this.subscription.request(1); | this.subscription.request(1); | ||||
} | } |