clean, constructeur, optimisation pour ne pas attendre un tick pour recréer les futures suivant
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@@ -10,13 +12,15 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
public class DiffusionAtomique implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
private Future<?>[] futures;
|
||||
private Queue<Integer> values = new LinkedList<>();
|
||||
private List<Future<?>> futures;
|
||||
private Queue<Integer> values;
|
||||
|
||||
|
||||
public DiffusionAtomique(CapteurImpl c){
|
||||
this.capteur = c;
|
||||
this.futures = new ArrayList<Future<?>>();
|
||||
this.values = new LinkedList<>();
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,27 +36,24 @@ public class DiffusionAtomique implements AlgoDiffusion {
|
||||
this.values.offer(this.capteur.getValue());
|
||||
}
|
||||
|
||||
if(this.futures==null){
|
||||
if(!this.values.isEmpty()){ //le capteur ne répond pas (car stopé ici)
|
||||
this.futures = new Future<?>[capteur.observers.size()];
|
||||
int i = 0;
|
||||
for (ObserverDeCapteurAsync canal : capteur.observers) {
|
||||
Future<?> f = canal.update();
|
||||
futures[i] = f;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
boolean allDone = true;
|
||||
for (Future<?> f : futures) {
|
||||
allDone = allDone && f.isDone();
|
||||
}
|
||||
if(allDone){
|
||||
this.futures = null;
|
||||
if(!this.futures.isEmpty()){
|
||||
//vérification si les Futures sont finis
|
||||
this.futures.removeIf(Future::isDone);
|
||||
if(this.futures.isEmpty()){
|
||||
this.values.remove();
|
||||
System.out.println("next value");
|
||||
}
|
||||
}
|
||||
|
||||
// si futures est empty, alors tous les futures canaux précédents sont finis
|
||||
// et on peut relancer des futures sans attendre le prochain ticket
|
||||
if(this.futures.isEmpty()){
|
||||
if(!this.values.isEmpty()){ //le capteur ne répond pas (car stopé ici)
|
||||
for (ObserverDeCapteurAsync canal : capteur.observers) {
|
||||
Future<?> f = canal.update();
|
||||
this.futures.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package impl;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
@@ -11,11 +10,12 @@ public class DiffusionEpoque implements AlgoDiffusion {
|
||||
public CapteurImpl capteur;
|
||||
private Future<?>[] futures;
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
public DiffusionEpoque(CapteurImpl capteur){
|
||||
this.capteur = capteur;
|
||||
this.futures = new Future<?>[capteur.observers.size()];
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() {
|
||||
return this.capteur.getValue();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
@@ -8,47 +10,33 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
public class DiffusionSequencielle implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
private Future<?>[] futures;
|
||||
private List<Future<?>> futures;
|
||||
private int value;
|
||||
private boolean valueSave = false;
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
public DiffusionSequencielle(CapteurImpl capteur){
|
||||
this.capteur = capteur;
|
||||
this.futures = new ArrayList<Future<?>>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValue(){
|
||||
if(!valueSave){
|
||||
this.value=this.capteur.getValue();
|
||||
this.valueSave = true;
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
if(this.futures == null){
|
||||
if(this.futures.isEmpty()){
|
||||
this.value = capteur.getValue();
|
||||
System.out.println("execute");
|
||||
this.futures = new Future<?>[capteur.observers.size()];
|
||||
int i = 0;
|
||||
for (ObserverDeCapteurAsync canal : capteur.observers) {
|
||||
Future<?> f = canal.update();
|
||||
this.futures[i] = f;
|
||||
i++;
|
||||
this.futures.add(f);
|
||||
}
|
||||
}
|
||||
else{
|
||||
boolean allDone = true;
|
||||
for (Future<?> f : futures) {
|
||||
allDone = allDone && f.isDone();
|
||||
}
|
||||
if(allDone){
|
||||
this.futures = null;
|
||||
valueSave = false;
|
||||
}
|
||||
futures.removeIf(Future::isDone);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class Scheduler {
|
||||
private final ScheduledExecutorService scheduler;
|
||||
|
||||
public Scheduler(int nbCanaux) {
|
||||
int nThread = 20+2*nbCanaux;
|
||||
int nThread = 2+2*nbCanaux;
|
||||
this.scheduler = Executors.newScheduledThreadPool(nThread);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@ package interfaces;
|
||||
|
||||
public interface AlgoDiffusion {
|
||||
|
||||
public void configure();
|
||||
|
||||
public void execute();
|
||||
|
||||
public int getValue();
|
||||
|
||||
@@ -18,12 +18,10 @@ public class main {
|
||||
Scheduler scheduler = new Scheduler(NB_CANAUX);
|
||||
CapteurImpl c = new CapteurImpl();
|
||||
|
||||
DiffusionAtomique algoAtom = new DiffusionAtomique();
|
||||
algoAtom.capteur = c;
|
||||
DiffusionSequencielle algoSeq = new DiffusionSequencielle();
|
||||
algoSeq.capteur = c;
|
||||
DiffusionEpoque algoEpoq = new DiffusionEpoque();
|
||||
algoEpoq.capteur = c;
|
||||
DiffusionAtomique algoAtom = new DiffusionAtomique(c);
|
||||
DiffusionSequencielle algoSeq = new DiffusionSequencielle(c);
|
||||
DiffusionEpoque algoEpoq = new DiffusionEpoque(c);
|
||||
//algoEpoq.capteur = c;
|
||||
|
||||
c.setAlgoDiffusion(algoEpoq);
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import impl.Scheduler;
|
||||
public class TestAlgoDiffusionAtom {
|
||||
|
||||
static int NB_CANAUX = 3;
|
||||
static int TEST_TIME = 30; // TEST_TIME/6 + TEST_TIME-TEST_TIME/6
|
||||
public static List<Integer>[] rAtom;
|
||||
|
||||
|
||||
@@ -37,8 +38,8 @@ public class TestAlgoDiffusionAtom {
|
||||
Scheduler scheduler = new Scheduler(NB_CANAUX);
|
||||
CapteurImpl c = new CapteurImpl();
|
||||
|
||||
DiffusionAtomique algo = new DiffusionAtomique();
|
||||
algo.capteur = c;
|
||||
DiffusionAtomique algo = new DiffusionAtomique(c);
|
||||
|
||||
c.setAlgoDiffusion(algo);
|
||||
|
||||
for(int i = 0; i<NB_CANAUX; i++){
|
||||
@@ -52,14 +53,20 @@ public class TestAlgoDiffusionAtom {
|
||||
ScheduledExecutorService clock = scheduler.getScheculer();
|
||||
ScheduledFuture<?> future = clock.scheduleAtFixedRate(() -> c.tick(), 0, 500, TimeUnit.MILLISECONDS);
|
||||
|
||||
Thread.sleep(10000);
|
||||
System.out.println("STOP");
|
||||
c.stop(); //arrête la mise à jour du capteur (mais stop pas le tick)
|
||||
System.out.println("waitting for lasts display...");
|
||||
for(int i = 60; i>0; i--){
|
||||
for(int i = TEST_TIME/6; i>0; i--){ //attend que le capteur génère des valeurs
|
||||
System.out.println("restant " + i + "s");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
System.out.println("STOP");
|
||||
c.stop(); //arrête la mise à jour du capteur (mais stop pas le tick)
|
||||
System.out.println("waitting for lasts display...");
|
||||
|
||||
for(int i = TEST_TIME-TEST_TIME/6; i>0; i--){
|
||||
System.out.println("restant " + i + "s");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
future.cancel(false);
|
||||
|
||||
clock.shutdown();
|
||||
|
||||
@@ -37,10 +37,6 @@ public class TestAlgoDiffusionEpoq {
|
||||
Scheduler scheduler = new Scheduler(NB_CANAUX);
|
||||
CapteurImpl c = new CapteurImpl();
|
||||
|
||||
DiffusionEpoque algo = new DiffusionEpoque();
|
||||
algo.capteur = c;
|
||||
c.setAlgoDiffusion(algo);
|
||||
|
||||
for(int i = 0; i<NB_CANAUX; i++){
|
||||
Afficheur afficheur = new Afficheur();
|
||||
afficheur.setId(i);
|
||||
@@ -49,7 +45,12 @@ public class TestAlgoDiffusionEpoq {
|
||||
c.attach(canal);
|
||||
}
|
||||
|
||||
algo.configure();
|
||||
DiffusionEpoque algo = new DiffusionEpoque(c);
|
||||
c.setAlgoDiffusion(algo);
|
||||
|
||||
|
||||
|
||||
//algo.configure();
|
||||
|
||||
ScheduledExecutorService clock = scheduler.getScheculer();
|
||||
ScheduledFuture<?> future = clock.scheduleAtFixedRate(() -> c.tick(), 0, 500, TimeUnit.MILLISECONDS);
|
||||
|
||||
@@ -37,8 +37,7 @@ public class TestAlgoDiffusionSeq {
|
||||
Scheduler scheduler = new Scheduler(NB_CANAUX);
|
||||
CapteurImpl c = new CapteurImpl();
|
||||
|
||||
DiffusionSequencielle algo = new DiffusionSequencielle();
|
||||
algo.capteur = c;
|
||||
DiffusionSequencielle algo = new DiffusionSequencielle(c);
|
||||
c.setAlgoDiffusion(algo);
|
||||
|
||||
for(int i = 0; i<NB_CANAUX; i++){
|
||||
|
||||
Reference in New Issue
Block a user