Browse Source

Generators implementiert

Jens
Jens Schuhmann 1 year ago
parent
commit
6ae236facd

src/bandit/Generator1.java → src/bandit/Model/Generator/Generator1.java View File

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/ */


package bandit;
package bandit.Model.Generator;


import bandit.Model.Zahlengenerator;
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;
{ {
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 Integer[] 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()
public Generator1(Zahlengenerator hauptgenerator)
{ {
wert = -1;
wert = new Integer[2];
wert[0] = -1;
wert[1] = 1;
laufend = false; laufend = false;
eService = Executors.newSingleThreadExecutor(); eService = Executors.newSingleThreadExecutor();
wertPublisher = new SubmissionPublisher<>(); wertPublisher = new SubmissionPublisher<>();
this.addWertSubscription(hauptgenerator);
LOCK = new Object(); LOCK = new Object();
} }
public void addWertSubscription(Subscriber<Integer> subscriber)
public void addWertSubscription(Subscriber<Integer[]> subscriber)
{ {
wertPublisher.subscribe(subscriber); wertPublisher.subscribe(subscriber);
} }
public void stop() public void stop()
{ {
laufend = false;
synchronized(LOCK){
laufend = false;
}
} }


@Override @Override


private synchronized void berechneWert() private synchronized void berechneWert()
{ {
wert = (int) (1 + 49*Math.random());
}
public synchronized int getWert()
{
return wert;
wert[0] = (int) (1 + 6*Math.random());
} }

} }

+ 114
- 0
src/bandit/Model/Generator/Generator2.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.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.notify();
}
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");
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());
}

}

+ 114
- 0
src/bandit/Model/Generator/Generator3.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.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.notify();
}
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");
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());
}

}

+ 112
- 0
src/bandit/Model/Zahlengenerator.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.Model;

import bandit.Model.Generator.Generator1;
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;
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 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;
private Flow.Subscription subscription;
private SubmissionPublisher<Integer[]> wertPublisher;

private Generator1 generator1;
private Thread thd1;
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);


}
public void addWertSubscription(Subscriber<Integer[]> subscriber)
{
wertPublisher.subscribe(subscriber);
}
public void start()
{
if(thd1==null){
thd1 = new Thread(generator1);
thd1.start();
}
if(thd2==null){
thd2 = new Thread(generator2);
thd2.start();
}
if(thd3==null){
thd3 = new Thread(generator3);
thd3.start();
}
generator1.start();
generator2.start();
generator3.start();


}
public void stop()
{
generator1.stop();
generator2.stop();
generator3.stop();
}



@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
}
}

src/bandit/StartStopController.java → src/bandit/Schnittstelle/StartStopController.java View File

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/ */


package bandit;
package bandit.Schnittstelle;


import bandit.Model.Zahlengenerator;
import bandit.View.ZahlenView;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.logging.Logger; import java.util.logging.Logger;

src/bandit/WertAdapter.java → src/bandit/Schnittstelle/WertAdapter.java View File

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/ */


package bandit;
package bandit.Schnittstelle;


import bandit.Model.Zahlengenerator;
import bandit.View.ZahlenView;
import java.util.concurrent.Flow; import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription; import java.util.concurrent.Flow.Subscription;
* *
* @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);
view.getLblZahl().setText(strWert);
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;
}
this.subscription.request(1); this.subscription.request(1);
} }



+ 5
- 0
src/bandit/Start.java View File



package bandit; package bandit;


import bandit.View.ZahlenView;
import bandit.Schnittstelle.WertAdapter;
import bandit.Schnittstelle.StartStopController;
import bandit.Model.Zahlengenerator;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.UIManager; import javax.swing.UIManager;


controller.registerEvents(); controller.registerEvents();
model.addWertSubscription(adapter); model.addWertSubscription(adapter);
view.setVisible(true); view.setVisible(true);
view.pack();


} }



src/bandit/ZahlenView.form → src/bandit/View/ZahlenView.form View File

</Property> </Property>
<Property name="horizontalAlignment" type="int" value="0"/> <Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="?"/> <Property name="text" type="java.lang.String" value="?"/>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
<LineBorder/>
</Border>
</Property>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lblZahl2"> <Component class="javax.swing.JLabel" name="lblZahl2">
</Property> </Property>
<Property name="horizontalAlignment" type="int" value="0"/> <Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="?"/> <Property name="text" type="java.lang.String" value="?"/>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
<LineBorder/>
</Border>
</Property>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lblZahl3"> <Component class="javax.swing.JLabel" name="lblZahl3">
</Property> </Property>
<Property name="horizontalAlignment" type="int" value="0"/> <Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="?"/> <Property name="text" type="java.lang.String" value="?"/>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
<LineBorder/>
</Border>
</Property>
</Properties> </Properties>
</Component> </Component>
</SubComponents> </SubComponents>

src/bandit/ZahlenView.java → src/bandit/View/ZahlenView.java View File

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/ */
package bandit;
package bandit.View;


/** /**
* *
lblZahl1.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N lblZahl1.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
lblZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblZahl1.setText("?"); lblZahl1.setText("?");
lblZahl1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(lblZahl1); jPanel2.add(lblZahl1);


lblZahl2.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N lblZahl2.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
lblZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblZahl2.setText("?"); lblZahl2.setText("?");
lblZahl2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(lblZahl2); jPanel2.add(lblZahl2);


lblZahl3.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N lblZahl3.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N
lblZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblZahl3.setText("?"); lblZahl3.setText("?");
lblZahl3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(lblZahl3); jPanel2.add(lblZahl3);


getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER); getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

+ 0
- 94
src/bandit/Zahlengenerator.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;
import java.util.concurrent.Flow;

/**
*
* @author le
*/
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;
private Flow.Subscription subscription;
private SubmissionPublisher<Integer> wertPublisher;

private Generator1 generator1;
private Thread thd1;
public Zahlengenerator()
{
wertPublisher = new SubmissionPublisher<>();
generator1 = new Generator1();
}
public void addWertSubscription(Subscriber<Integer> subscriber)
{
wertPublisher.subscribe(subscriber);
}
public void start()
{
if(thd1==null){
thd1 = new Thread(generator1);
thd1.start();
}
generator1.start();
}
public void stop()
{
generator1.stop();
}



private synchronized void berechneWert()
{
wert = (int) (1 + 49*Math.random());
}
public synchronized int getWert()
{
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