Test
This commit is contained in:
parent
1e899d7e62
commit
5be14364fd
@ -22,19 +22,18 @@ public class Generator1 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 int wert;
|
||||
private volatile boolean laufend;
|
||||
private ExecutorService eService;
|
||||
|
||||
private final Object LOCK;
|
||||
|
||||
private SubmissionPublisher<Integer[]> wertPublisher;
|
||||
private SubmissionPublisher<Integer> wertPublisher;
|
||||
|
||||
public Generator1(Zahlengenerator hauptgenerator)
|
||||
{
|
||||
wert = new Integer[2];
|
||||
wert[0] = -1;
|
||||
wert[1] = 1;
|
||||
wert = 0;
|
||||
|
||||
|
||||
laufend = false;
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
@ -43,7 +42,7 @@ public class Generator1 implements Runnable // Callable
|
||||
LOCK = new Object();
|
||||
}
|
||||
|
||||
public void addWertSubscription(Subscriber<Integer[]> subscriber)
|
||||
public void addWertSubscription(Subscriber<Integer> subscriber)
|
||||
{
|
||||
wertPublisher.subscribe(subscriber);
|
||||
}
|
||||
@ -74,6 +73,7 @@ public class Generator1 implements Runnable // Callable
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
lg.info("Generator1 run");
|
||||
while (true)
|
||||
{
|
||||
while (!laufend)
|
||||
@ -107,7 +107,7 @@ public class Generator1 implements Runnable // Callable
|
||||
|
||||
private synchronized void berechneWert()
|
||||
{
|
||||
wert[0] = (int) (1 + 6*Math.random());
|
||||
wert = (int) (1 + 6*Math.random());
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -6,8 +6,8 @@
|
||||
package bandit.Model;
|
||||
|
||||
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.Executors;
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
@ -21,34 +21,34 @@ import java.util.concurrent.Flow;
|
||||
*
|
||||
* @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 Flow.Subscription subscription;
|
||||
private SubmissionPublisher<Integer[]> wertPublisher;
|
||||
private SubmissionPublisher<Integer> wertPublisher;
|
||||
|
||||
private Generator1 generator1;
|
||||
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()
|
||||
{
|
||||
wertPublisher = new SubmissionPublisher<>();
|
||||
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);
|
||||
}
|
||||
@ -57,20 +57,22 @@ public class Zahlengenerator implements Subscriber<Integer[]> // Callable
|
||||
{
|
||||
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();
|
||||
}
|
||||
// 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();
|
||||
//generator2.start();
|
||||
//generator3.start();
|
||||
|
||||
|
||||
}
|
||||
@ -78,8 +80,8 @@ public class Zahlengenerator implements Subscriber<Integer[]> // Callable
|
||||
public void stop()
|
||||
{
|
||||
generator1.stop();
|
||||
generator2.stop();
|
||||
generator3.stop();
|
||||
//generator2.stop();
|
||||
//generator3.stop();
|
||||
}
|
||||
|
||||
|
||||
@ -93,7 +95,7 @@ public void onSubscribe(Flow.Subscription subscription) {
|
||||
|
||||
|
||||
@Override
|
||||
public void onNext(Integer[] item) {
|
||||
public void onNext(Integer item) {
|
||||
this.wertPublisher.submit(item);
|
||||
this.subscription.request(1);
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import java.util.concurrent.Flow.Subscription;
|
||||
*
|
||||
* @author le
|
||||
*/
|
||||
public class WertAdapter implements Subscriber<Integer[]>
|
||||
public class WertAdapter implements Subscriber<Integer>
|
||||
{
|
||||
private ZahlenView view;
|
||||
private Zahlengenerator model;
|
||||
@ -35,23 +35,12 @@ public class WertAdapter implements Subscriber<Integer[]>
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user