Wuerfel wuerfelt
This commit is contained in:
parent
50deb6c1c9
commit
13483d909c
@ -22,50 +22,51 @@ public class Wuerfel implements Runnable // Callable
|
||||
{
|
||||
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 ExecutorService eService;
|
||||
|
||||
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;
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
eService = null;
|
||||
wertPublisher = new SubmissionPublisher<>();
|
||||
this.addWertSubscription(hauptgenerator);
|
||||
LOCK = new Object();
|
||||
}
|
||||
|
||||
public void addWertSubscription(Subscriber<Integer> subscriber)
|
||||
public void addWertSubscription(Subscriber<WuerfelData> subscriber)
|
||||
{
|
||||
wertPublisher.subscribe(subscriber);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
laufend = true;
|
||||
synchronized (this) {
|
||||
laufend = true;
|
||||
}
|
||||
|
||||
if (eService == null)
|
||||
{
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
eService.execute(this);
|
||||
}
|
||||
synchronized (LOCK)
|
||||
{
|
||||
LOCK.notifyAll();
|
||||
}
|
||||
|
||||
|
||||
if (eService.isShutdown())
|
||||
{
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
eService.execute(this);
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
synchronized(LOCK){
|
||||
synchronized(this){
|
||||
laufend = false;
|
||||
}
|
||||
}
|
||||
@ -101,14 +102,17 @@ public class Wuerfel implements Runnable // Callable
|
||||
System.err.println(ex);
|
||||
}
|
||||
this.berechneWert();
|
||||
wertPublisher.submit(wert);
|
||||
// lg.info("submit " + data.getValue());
|
||||
wertPublisher.submit(data);
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void berechneWert()
|
||||
{
|
||||
wert = (int) (1 + 6*Math.random());
|
||||
data.setValue((int) (1 + 6*Math.random()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
44
src/bandit/Model/WuerfelData.java
Normal file
44
src/bandit/Model/WuerfelData.java
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -14,73 +14,74 @@ import java.util.concurrent.SubmissionPublisher;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
import bandit.util.OhmLogger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Flow;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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 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()
|
||||
{
|
||||
subscriptions = new ArrayList<>();
|
||||
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);
|
||||
}
|
||||
|
||||
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("Generator gestartet");
|
||||
|
||||
wuerfel_1.start();
|
||||
wuerfel_2.start();
|
||||
wuerfel_3.start();
|
||||
|
||||
lg.info("generator1.start");
|
||||
generator1.start();
|
||||
//generator2.start();
|
||||
//generator3.start();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
generator1.stop();
|
||||
//generator2.stop();
|
||||
//generator3.stop();
|
||||
lg.info("Generator gestopt");
|
||||
|
||||
wuerfel_1.stop();
|
||||
wuerfel_2.stop();
|
||||
wuerfel_3.stop();
|
||||
}
|
||||
|
||||
|
||||
@ -88,15 +89,17 @@ public class Zahlengenerator implements Subscriber<Integer> // Callable
|
||||
|
||||
@Override
|
||||
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
|
||||
public void onNext(Integer item) {
|
||||
public void onNext(WuerfelData item) {
|
||||
lg.info("on Next");
|
||||
this.wertPublisher.submit(item);
|
||||
this.subscription.request(1);
|
||||
this.subscriptions.get(item.getId() - 1).request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
package bandit.Schnittstelle;
|
||||
|
||||
import bandit.Model.WuerfelData;
|
||||
import bandit.Model.Zahlengenerator;
|
||||
import bandit.View.ZahlenView;
|
||||
import java.util.concurrent.Flow;
|
||||
@ -15,7 +16,7 @@ import java.util.concurrent.Flow.Subscription;
|
||||
*
|
||||
* @author le
|
||||
*/
|
||||
public class WertAdapter implements Subscriber<Integer>
|
||||
public class WertAdapter implements Subscriber<WuerfelData>
|
||||
{
|
||||
private ZahlenView view;
|
||||
private Zahlengenerator model;
|
||||
@ -35,10 +36,23 @@ public class WertAdapter implements Subscriber<Integer>
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user