1 - First Try
This commit is contained in:
commit
7d90085271
88
src/subscriber/Datenmodell3.java
Normal file
88
src/subscriber/Datenmodell3.java
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package subscriber;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.SubmissionPublisher;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author le
|
||||
*/
|
||||
public class Datenmodell3 implements Runnable
|
||||
{
|
||||
private int faktor;
|
||||
private int wert;
|
||||
private volatile boolean laufend;
|
||||
private SubmissionPublisher<Integer> iPublisher;
|
||||
private ExecutorService eService;
|
||||
|
||||
public Datenmodell3()
|
||||
{
|
||||
faktor = 10;
|
||||
wert = 1;
|
||||
iPublisher = new SubmissionPublisher<>();
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
laufend = true;
|
||||
eService.submit(this);
|
||||
try{
|
||||
wait();
|
||||
}
|
||||
catch(InterruptedException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
notify();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
try{
|
||||
wait();
|
||||
}
|
||||
catch(InterruptedException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void terminate(){
|
||||
laufend = false;
|
||||
}
|
||||
|
||||
public void addWertSubscription(Subscriber<Integer> subscriber)
|
||||
{
|
||||
iPublisher.subscribe(subscriber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (laufend)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(5);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println(e);
|
||||
}
|
||||
wert = calculateWert();
|
||||
System.out.println(wert); // just for displaying that the thread is running
|
||||
iPublisher.submit(wert);
|
||||
}
|
||||
}
|
||||
|
||||
public int calculateWert(){
|
||||
return (int)(Math.random()*6) + 1;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user