{ | { | ||||
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 Integer[] wert; | |||||
private int wert; | |||||
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<Integer> wertPublisher; | |||||
public Generator1(Zahlengenerator hauptgenerator) | public Generator1(Zahlengenerator hauptgenerator) | ||||
{ | { | ||||
wert = new Integer[2]; | |||||
wert[0] = -1; | |||||
wert[1] = 1; | |||||
wert = 0; | |||||
laufend = false; | laufend = false; | ||||
eService = Executors.newSingleThreadExecutor(); | eService = Executors.newSingleThreadExecutor(); | ||||
LOCK = new Object(); | LOCK = new Object(); | ||||
} | } | ||||
public void addWertSubscription(Subscriber<Integer[]> subscriber) | |||||
public void addWertSubscription(Subscriber<Integer> subscriber) | |||||
{ | { | ||||
wertPublisher.subscribe(subscriber); | wertPublisher.subscribe(subscriber); | ||||
} | } | ||||
@Override | @Override | ||||
public void run() | public void run() | ||||
{ | { | ||||
lg.info("Generator1 run"); | |||||
while (true) | while (true) | ||||
{ | { | ||||
while (!laufend) | while (!laufend) | ||||
private synchronized void berechneWert() | private synchronized void berechneWert() | ||||
{ | { | ||||
wert[0] = (int) (1 + 6*Math.random()); | |||||
wert = (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.Generator; | |||||
import bandit.Model.Zahlengenerator; | |||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.concurrent.Flow.Subscriber; | |||||
import java.util.concurrent.SubmissionPublisher; | |||||
import java.util.concurrent.atomic.AtomicBoolean; | |||||
import java.util.logging.Logger; | |||||
import bandit.util.OhmLogger; | |||||
/** | |||||
* | |||||
* @author le | |||||
*/ | |||||
public class Generator2 implements Runnable // Callable | |||||
{ | |||||
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | |||||
private Integer[] wert; | |||||
private volatile boolean laufend; | |||||
private ExecutorService eService; | |||||
private final Object LOCK; | |||||
private SubmissionPublisher<Integer[]> wertPublisher; | |||||
public Generator2(Zahlengenerator hauptgenerator) | |||||
{ | |||||
wert = new Integer[2]; | |||||
wert[0] = -1; | |||||
wert[1] = 2; | |||||
laufend = false; | |||||
eService = Executors.newSingleThreadExecutor(); | |||||
wertPublisher = new SubmissionPublisher<>(); | |||||
this.addWertSubscription(hauptgenerator); | |||||
LOCK = new Object(); | |||||
} | |||||
public void addWertSubscription(Subscriber<Integer[]> subscriber) | |||||
{ | |||||
wertPublisher.subscribe(subscriber); | |||||
} | |||||
public void start() | |||||
{ | |||||
laufend = true; | |||||
synchronized (LOCK) | |||||
{ | |||||
LOCK.notifyAll(); | |||||
} | |||||
if (eService.isShutdown()) | |||||
{ | |||||
eService = Executors.newSingleThreadExecutor(); | |||||
} | |||||
eService.execute(this); | |||||
} | |||||
public void stop() | |||||
{ | |||||
synchronized(LOCK){ | |||||
laufend = false; | |||||
} | |||||
} | |||||
@Override | |||||
public void run() | |||||
{ | |||||
while (true) | |||||
{ | |||||
while (!laufend) | |||||
{ | |||||
synchronized (LOCK) | |||||
{ | |||||
try | |||||
{ | |||||
lg.info("WAIT_Gen2"); | |||||
LOCK.wait(); | |||||
} | |||||
catch (InterruptedException ex) | |||||
{ | |||||
lg.warning(ex.toString()); | |||||
} | |||||
} | |||||
} | |||||
try | |||||
{ | |||||
Thread.sleep(100); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
System.err.println(ex); | |||||
} | |||||
this.berechneWert(); | |||||
wertPublisher.submit(wert); | |||||
} | |||||
} | |||||
private synchronized void berechneWert() | |||||
{ | |||||
wert[0] = (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.Generator; | |||||
import bandit.Model.Zahlengenerator; | |||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.concurrent.Flow.Subscriber; | |||||
import java.util.concurrent.SubmissionPublisher; | |||||
import java.util.concurrent.atomic.AtomicBoolean; | |||||
import java.util.logging.Logger; | |||||
import bandit.util.OhmLogger; | |||||
/** | |||||
* | |||||
* @author le | |||||
*/ | |||||
public class Generator3 implements Runnable // Callable | |||||
{ | |||||
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | |||||
private Integer[] wert; | |||||
private volatile boolean laufend; | |||||
private ExecutorService eService; | |||||
private final Object LOCK; | |||||
private SubmissionPublisher<Integer[]> wertPublisher; | |||||
public Generator3(Zahlengenerator hauptgenerator) | |||||
{ | |||||
wert = new Integer[2]; | |||||
wert[0] = -1; | |||||
wert[1] = 3; | |||||
laufend = false; | |||||
eService = Executors.newSingleThreadExecutor(); | |||||
wertPublisher = new SubmissionPublisher<>(); | |||||
this.addWertSubscription(hauptgenerator); | |||||
LOCK = new Object(); | |||||
} | |||||
public void addWertSubscription(Subscriber<Integer[]> subscriber) | |||||
{ | |||||
wertPublisher.subscribe(subscriber); | |||||
} | |||||
public void start() | |||||
{ | |||||
laufend = true; | |||||
synchronized (LOCK) | |||||
{ | |||||
LOCK.notifyAll(); | |||||
} | |||||
if (eService.isShutdown()) | |||||
{ | |||||
eService = Executors.newSingleThreadExecutor(); | |||||
} | |||||
eService.execute(this); | |||||
} | |||||
public void stop() | |||||
{ | |||||
synchronized(LOCK){ | |||||
laufend = false; | |||||
} | |||||
} | |||||
@Override | |||||
public void run() | |||||
{ | |||||
while (true) | |||||
{ | |||||
while (!laufend) | |||||
{ | |||||
synchronized (LOCK) | |||||
{ | |||||
try | |||||
{ | |||||
lg.info("WAIT_Gen3"); | |||||
LOCK.wait(); | |||||
} | |||||
catch (InterruptedException ex) | |||||
{ | |||||
lg.warning(ex.toString()); | |||||
} | |||||
} | |||||
} | |||||
try | |||||
{ | |||||
Thread.sleep(100); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
System.err.println(ex); | |||||
} | |||||
this.berechneWert(); | |||||
wertPublisher.submit(wert); | |||||
} | |||||
} | |||||
private synchronized void berechneWert() | |||||
{ | |||||
wert[0] = (int) (1 + 6*Math.random()); | |||||
} | |||||
} |
package bandit.Model; | package bandit.Model; | ||||
import bandit.Model.Generator.Generator1; | import bandit.Model.Generator.Generator1; | ||||
import bandit.Model.Generator.Generator2; | |||||
import bandit.Model.Generator.Generator3; | |||||
//import bandit.Model.Generator.Generator2; | |||||
//import bandit.Model.Generator.Generator3; | |||||
import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | ||||
import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||
import java.util.concurrent.Flow.Subscriber; | import java.util.concurrent.Flow.Subscriber; | ||||
* | * | ||||
* @author le | * @author le | ||||
*/ | */ | ||||
public class Zahlengenerator implements Subscriber<Integer[]> // Callable | |||||
public class Zahlengenerator implements Subscriber<Integer> // 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 Flow.Subscription subscription; | ||||
private SubmissionPublisher<Integer[]> wertPublisher; | |||||
private SubmissionPublisher<Integer> wertPublisher; | |||||
private Generator1 generator1; | private Generator1 generator1; | ||||
private Thread thd1; | private Thread thd1; | ||||
private Generator2 generator2; | |||||
private Thread thd2; | |||||
private Generator3 generator3; | |||||
private Thread thd3; | |||||
// private Generator2 generator2; | |||||
// private Thread thd2; | |||||
// | |||||
// private Generator3 generator3; | |||||
// private Thread thd3; | |||||
// | |||||
public Zahlengenerator() | public Zahlengenerator() | ||||
{ | { | ||||
wertPublisher = new SubmissionPublisher<>(); | wertPublisher = new SubmissionPublisher<>(); | ||||
generator1 = new Generator1(this); | generator1 = new Generator1(this); | ||||
generator2 = new Generator2(this); | |||||
generator3 = new Generator3(this); | |||||
// generator2 = new Generator2(this); | |||||
// generator3 = new Generator3(this); | |||||
} | } | ||||
public void addWertSubscription(Subscriber<Integer[]> subscriber) | |||||
public void addWertSubscription(Subscriber<Integer> subscriber) | |||||
{ | { | ||||
wertPublisher.subscribe(subscriber); | wertPublisher.subscribe(subscriber); | ||||
} | } | ||||
{ | { | ||||
if(thd1==null){ | if(thd1==null){ | ||||
thd1 = new Thread(generator1); | thd1 = new Thread(generator1); | ||||
lg.info("thd1.start"); | |||||
thd1.start(); | thd1.start(); | ||||
} | } | ||||
if(thd2==null){ | |||||
thd2 = new Thread(generator2); | |||||
thd2.start(); | |||||
} | |||||
if(thd3==null){ | |||||
thd3 = new Thread(generator3); | |||||
thd3.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(); | generator1.start(); | ||||
generator2.start(); | |||||
generator3.start(); | |||||
//generator2.start(); | |||||
//generator3.start(); | |||||
} | } | ||||
public void stop() | public void stop() | ||||
{ | { | ||||
generator1.stop(); | generator1.stop(); | ||||
generator2.stop(); | |||||
generator3.stop(); | |||||
//generator2.stop(); | |||||
//generator3.stop(); | |||||
} | } | ||||
@Override | @Override | ||||
public void onNext(Integer[] item) { | |||||
public void onNext(Integer item) { | |||||
this.wertPublisher.submit(item); | this.wertPublisher.submit(item); | ||||
this.subscription.request(1); | this.subscription.request(1); | ||||
} | } |
* | * | ||||
* @author le | * @author le | ||||
*/ | */ | ||||
public class WertAdapter implements Subscriber<Integer[]> | |||||
public class WertAdapter implements Subscriber<Integer> | |||||
{ | { | ||||
private ZahlenView view; | private ZahlenView view; | ||||
private Zahlengenerator model; | private Zahlengenerator model; | ||||
} | } | ||||
@Override | @Override | ||||
public void onNext(Integer item[]) | |||||
public void onNext(Integer item) | |||||
{ | { | ||||
String strWert = String.valueOf(item[0]); | |||||
switch(item[1]){ | |||||
case 1: | |||||
view.getLblZahl1().setText(strWert); | |||||
break; | |||||
case 2: | |||||
view.getLblZahl2().setText(strWert); | |||||
break; | |||||
case 3: | |||||
view.getLblZahl3().setText(strWert); | |||||
break; | |||||
} | |||||
String strWert = String.valueOf(item); | |||||
view.getLblZahl1().setText(strWert); | |||||
this.subscription.request(1); | this.subscription.request(1); | ||||
} | } | ||||
@Override | @Override |