Browse Source

1 - First Try

master
marku 5 years ago
commit
7d90085271
1 changed files with 88 additions and 0 deletions
  1. 88
    0
      src/subscriber/Datenmodell3.java

+ 88
- 0
src/subscriber/Datenmodell3.java View File

/*
* 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…
Cancel
Save