Browse Source

Generator Model gekapselt

Jens
Jens Schuhmann 1 year ago
parent
commit
d8454b90de

+ 111
- 0
src/bandit/Generator1.java View File

/*
* 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;
}
}

+ 0
- 1
src/bandit/WertAdapter.java View File

{ {
this.subscription = subscription; this.subscription = subscription;
this.subscription.request(1); this.subscription.request(1);
//this.subscription.request(Long.MAX_VALUE);
} }


@Override @Override

+ 38
- 14
src/bandit/ZahlenView.form View File



<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<SubComponents> <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"> <Container class="javax.swing.JPanel" name="jPanel1">
<Constraints> <Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
</Component> </Component>
</SubComponents> </SubComponents>
</Container> </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> </SubComponents>
</Form> </Form>

+ 47
- 8
src/bandit/ZahlenView.java View File

*/ */
public class ZahlenView extends javax.swing.JFrame 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 * @return the btnStart
*/ */
*/ */
public javax.swing.JLabel getLblZahl() public javax.swing.JLabel getLblZahl()
{ {
return lblZahl;
return getLblZahl1();
} }


/** /**
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() { private void initComponents() {


lblZahl = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel(); jPanel1 = new javax.swing.JPanel();
btnStart = new javax.swing.JButton(); btnStart = new javax.swing.JButton();
btnStop = 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); 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"); btnStart.setText("Start");
jPanel1.add(btnStart); jPanel1.add(btnStart);




getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END); 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(); pack();
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents


private javax.swing.JButton btnStart; private javax.swing.JButton btnStart;
private javax.swing.JButton btnStop; private javax.swing.JButton btnStop;
private javax.swing.JPanel jPanel1; 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 // End of variables declaration//GEN-END:variables
} }

+ 40
- 75
src/bandit/Zahlengenerator.java View File

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.concurrent.Flow;


/** /**
* *
* @author le * @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 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;
/** 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 SubmissionPublisher<Integer> wertPublisher;

private Generator1 generator1;
private Thread thd1;
public Zahlengenerator() 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) public void addWertSubscription(Subscriber<Integer> subscriber)
public void start() public void start()
{ {
//laufend = true;
laufend.set(true);
synchronized (LOCK)
{
LOCK.notify();
}
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();
// }
if(thd1==null){
thd1 = new Thread(generator1);
thd1.start();
}
generator1.start();
} }
public void stop() 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() private synchronized void berechneWert()
{ {
{ {
return wert; 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…
Cancel
Save