Generator Model gekapselt
This commit is contained in:
parent
cc8f445dc0
commit
d8454b90de
111
src/bandit/Generator1.java
Normal file
111
src/bandit/Generator1.java
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
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 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 int wert;
|
||||
private volatile boolean laufend;
|
||||
private ExecutorService eService;
|
||||
|
||||
private final Object LOCK;
|
||||
|
||||
private SubmissionPublisher<Integer> wertPublisher;
|
||||
|
||||
public Generator1()
|
||||
{
|
||||
wert = -1;
|
||||
laufend = false;
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
wertPublisher = new SubmissionPublisher<>();
|
||||
LOCK = new Object();
|
||||
}
|
||||
|
||||
public void addWertSubscription(Subscriber<Integer> subscriber)
|
||||
{
|
||||
wertPublisher.subscribe(subscriber);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
laufend = true;
|
||||
synchronized (LOCK)
|
||||
{
|
||||
LOCK.notify();
|
||||
}
|
||||
|
||||
|
||||
if (eService.isShutdown())
|
||||
{
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
eService.execute(this);
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
laufend = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
while (!laufend)
|
||||
{
|
||||
synchronized (LOCK)
|
||||
{
|
||||
try
|
||||
{
|
||||
lg.info("WAIT");
|
||||
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 = (int) (1 + 49*Math.random());
|
||||
}
|
||||
|
||||
public synchronized int getWert()
|
||||
{
|
||||
return wert;
|
||||
}
|
||||
|
||||
}
|
@ -30,7 +30,6 @@ public class WertAdapter implements Subscriber<Integer>
|
||||
{
|
||||
this.subscription = subscription;
|
||||
this.subscription.request(1);
|
||||
//this.subscription.request(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,20 +23,6 @@
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
|
||||
<SubComponents>
|
||||
<Component class="javax.swing.JLabel" name="lblZahl">
|
||||
<Properties>
|
||||
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
|
||||
<Font name="Liberation Sans" size="100" style="0"/>
|
||||
</Property>
|
||||
<Property name="horizontalAlignment" type="int" value="0"/>
|
||||
<Property name="text" type="java.lang.String" value="?"/>
|
||||
</Properties>
|
||||
<Constraints>
|
||||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
|
||||
<BorderConstraints direction="Center"/>
|
||||
</Constraint>
|
||||
</Constraints>
|
||||
</Component>
|
||||
<Container class="javax.swing.JPanel" name="jPanel1">
|
||||
<Constraints>
|
||||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
|
||||
@ -58,5 +44,43 @@
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Container>
|
||||
<Container class="javax.swing.JPanel" name="jPanel2">
|
||||
<Constraints>
|
||||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
|
||||
<BorderConstraints direction="Center"/>
|
||||
</Constraint>
|
||||
</Constraints>
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
|
||||
<SubComponents>
|
||||
<Component class="javax.swing.JLabel" name="lblZahl1">
|
||||
<Properties>
|
||||
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
|
||||
<Font name="Liberation Sans" size="100" style="0"/>
|
||||
</Property>
|
||||
<Property name="horizontalAlignment" type="int" value="0"/>
|
||||
<Property name="text" type="java.lang.String" value="?"/>
|
||||
</Properties>
|
||||
</Component>
|
||||
<Component class="javax.swing.JLabel" name="lblZahl2">
|
||||
<Properties>
|
||||
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
|
||||
<Font name="Liberation Sans" size="100" style="0"/>
|
||||
</Property>
|
||||
<Property name="horizontalAlignment" type="int" value="0"/>
|
||||
<Property name="text" type="java.lang.String" value="?"/>
|
||||
</Properties>
|
||||
</Component>
|
||||
<Component class="javax.swing.JLabel" name="lblZahl3">
|
||||
<Properties>
|
||||
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
|
||||
<Font name="Liberation Sans" size="100" style="0"/>
|
||||
</Property>
|
||||
<Property name="horizontalAlignment" type="int" value="0"/>
|
||||
<Property name="text" type="java.lang.String" value="?"/>
|
||||
</Properties>
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Container>
|
||||
</SubComponents>
|
||||
</Form>
|
||||
|
@ -10,6 +10,27 @@ package bandit;
|
||||
*/
|
||||
public class ZahlenView extends javax.swing.JFrame
|
||||
{
|
||||
|
||||
/**
|
||||
* @return the lblZahl1
|
||||
*/
|
||||
public javax.swing.JLabel getLblZahl1() {
|
||||
return lblZahl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the lblZahl2
|
||||
*/
|
||||
public javax.swing.JLabel getLblZahl2() {
|
||||
return lblZahl2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the lblZahl3
|
||||
*/
|
||||
public javax.swing.JLabel getLblZahl3() {
|
||||
return lblZahl3;
|
||||
}
|
||||
/**
|
||||
* @return the btnStart
|
||||
*/
|
||||
@ -31,7 +52,7 @@ public class ZahlenView extends javax.swing.JFrame
|
||||
*/
|
||||
public javax.swing.JLabel getLblZahl()
|
||||
{
|
||||
return lblZahl;
|
||||
return getLblZahl1();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,18 +72,16 @@ public class ZahlenView extends javax.swing.JFrame
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
||||
private void initComponents() {
|
||||
|
||||
lblZahl = new javax.swing.JLabel();
|
||||
jPanel1 = new javax.swing.JPanel();
|
||||
btnStart = new javax.swing.JButton();
|
||||
btnStop = new javax.swing.JButton();
|
||||
jPanel2 = new javax.swing.JPanel();
|
||||
lblZahl1 = new javax.swing.JLabel();
|
||||
lblZahl2 = new javax.swing.JLabel();
|
||||
lblZahl3 = new javax.swing.JLabel();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
lblZahl.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
|
||||
lblZahl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
lblZahl.setText("?");
|
||||
getContentPane().add(lblZahl, java.awt.BorderLayout.CENTER);
|
||||
|
||||
btnStart.setText("Start");
|
||||
jPanel1.add(btnStart);
|
||||
|
||||
@ -71,6 +90,23 @@ public class ZahlenView extends javax.swing.JFrame
|
||||
|
||||
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
|
||||
|
||||
lblZahl1.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
|
||||
lblZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
lblZahl1.setText("?");
|
||||
jPanel2.add(lblZahl1);
|
||||
|
||||
lblZahl2.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
|
||||
lblZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
lblZahl2.setText("?");
|
||||
jPanel2.add(lblZahl2);
|
||||
|
||||
lblZahl3.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
|
||||
lblZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
lblZahl3.setText("?");
|
||||
jPanel2.add(lblZahl3);
|
||||
|
||||
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
|
||||
|
||||
pack();
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
@ -127,6 +163,9 @@ public class ZahlenView extends javax.swing.JFrame
|
||||
private javax.swing.JButton btnStart;
|
||||
private javax.swing.JButton btnStop;
|
||||
private javax.swing.JPanel jPanel1;
|
||||
private javax.swing.JLabel lblZahl;
|
||||
private javax.swing.JPanel jPanel2;
|
||||
private javax.swing.JLabel lblZahl1;
|
||||
private javax.swing.JLabel lblZahl2;
|
||||
private javax.swing.JLabel lblZahl3;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
}
|
||||
|
@ -12,37 +12,28 @@ import java.util.concurrent.SubmissionPublisher;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
import bandit.util.OhmLogger;
|
||||
import java.util.concurrent.Flow;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author le
|
||||
*/
|
||||
public class Zahlengenerator implements Runnable // 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 int wert;
|
||||
/** Zustandsvariable !!!
|
||||
* bei 2 Werten boolean sonst enum
|
||||
*/
|
||||
// private volatile boolean laufend;
|
||||
private AtomicBoolean laufend;
|
||||
//private Thread thd;
|
||||
private ExecutorService eService;
|
||||
|
||||
private final Object LOCK;
|
||||
|
||||
private Flow.Subscription subscription;
|
||||
private SubmissionPublisher<Integer> wertPublisher;
|
||||
|
||||
private Generator1 generator1;
|
||||
private Thread thd1;
|
||||
|
||||
|
||||
public Zahlengenerator()
|
||||
{
|
||||
wert = -1;
|
||||
//laufend = false;
|
||||
laufend = new AtomicBoolean(false);
|
||||
//thd = null;
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
wertPublisher = new SubmissionPublisher<>();
|
||||
LOCK = new Object();
|
||||
wertPublisher = new SubmissionPublisher<>();
|
||||
generator1 = new Generator1();
|
||||
}
|
||||
|
||||
public void addWertSubscription(Subscriber<Integer> subscriber)
|
||||
@ -52,69 +43,21 @@ public class Zahlengenerator implements Runnable // Callable
|
||||
|
||||
public void start()
|
||||
{
|
||||
//laufend = true;
|
||||
laufend.set(true);
|
||||
synchronized (LOCK)
|
||||
{
|
||||
LOCK.notify();
|
||||
}
|
||||
if(thd1==null){
|
||||
thd1 = new Thread(generator1);
|
||||
thd1.start();
|
||||
}
|
||||
|
||||
generator1.start();
|
||||
|
||||
if (eService.isShutdown())
|
||||
{
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
eService.execute(this);
|
||||
//Future task = eService.submit(this); v.a. bei callable
|
||||
// if (thd == null)
|
||||
// {
|
||||
// thd = new Thread(this);
|
||||
// thd.start();
|
||||
// }
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
//laufend = false;
|
||||
laufend.set(false);
|
||||
generator1.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
//while (!laufend)
|
||||
while (!laufend.get())
|
||||
{
|
||||
synchronized (LOCK)
|
||||
{
|
||||
try
|
||||
{
|
||||
lg.info("WAIT");
|
||||
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);
|
||||
}
|
||||
// thd = null;
|
||||
// eService.shutdown();
|
||||
}
|
||||
|
||||
private synchronized void berechneWert()
|
||||
{
|
||||
@ -126,4 +69,26 @@ public class Zahlengenerator implements Runnable // Callable
|
||||
return wert;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Flow.Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
this.subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Integer item) {
|
||||
this.wertPublisher.submit(item);
|
||||
this.subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user