correction algo diffusion

This commit is contained in:
trochas
2025-11-26 11:17:53 +01:00
parent 1659650450
commit 3684570a83
9 changed files with 40 additions and 46 deletions

View File

@@ -10,7 +10,6 @@ import java.util.concurrent.ScheduledExecutorService;
public class CapteurImpl implements Capteur { public class CapteurImpl implements Capteur {
private ScheduledExecutorService scheduler;
private int value = 0; private int value = 0;
private AlgoDiffusion diffusion; private AlgoDiffusion diffusion;
protected List<ObserverDeCapteurAsync> observers=new ArrayList<ObserverDeCapteurAsync>(); protected List<ObserverDeCapteurAsync> observers=new ArrayList<ObserverDeCapteurAsync>();
@@ -30,16 +29,14 @@ public class CapteurImpl implements Capteur {
} }
@Override @Override
public void tick() { public void tick() {
System.out.println("tick call");
if(!locker){ if(!locker){
this.value++; this.value++;
System.out.println("tick(" + this.value+")\t\t" + (System.currentTimeMillis()-initialTime) + "ms");
//diffusion.execute();
if(!locker2){
scheduler.execute(() -> diffusion.execute());
}
} }
System.out.println("tick(" + this.value+")\t\t" + (System.currentTimeMillis()-initialTime) + "ms" + " locker : " + locker);
diffusion.execute();
} }
public synchronized void lock(){ public synchronized void lock(){
@@ -67,10 +64,10 @@ public class CapteurImpl implements Capteur {
return this.value; return this.value;
} }
public void setScheduler(ScheduledExecutorService scheduler){ @Override
this.scheduler = scheduler; public int getValueDiffusion(){
return this.diffusion.getValue();
} }
} }

View File

@@ -9,6 +9,9 @@ import interfaces.ObserverDeCapteurAsync;
public class DiffusionSequencielle implements AlgoDiffusion { public class DiffusionSequencielle implements AlgoDiffusion {
public CapteurImpl capteur; public CapteurImpl capteur;
private Future<?>[] futures;
private int value;
private boolean valueSave = false;
@Override @Override
public void configure() { public void configure() {
@@ -16,41 +19,37 @@ public class DiffusionSequencielle implements AlgoDiffusion {
throw new UnsupportedOperationException("Unimplemented method 'configure'"); throw new UnsupportedOperationException("Unimplemented method 'configure'");
} }
@Override
public int getValue(){
if(!valueSave){
this.value=this.capteur.getValue();
this.valueSave = true;
}
return this.value;
}
@Override @Override
public void execute() { public void execute() {
this.capteur.lock2();
System.out.println("execute");
Future<?>[] l = new Future<?>[capteur.observers.size()];
int i = 0;
for (ObserverDeCapteurAsync canal : capteur.observers) {
Future<?> f = canal.update();
l[i] = f;
i++;
}
boolean firstDone = false;
while(!firstDone){ //attend que le premier futur soit résolu if(futures == null){
for (Future<?> f : l) { System.out.println("execute");
if(f.isDone()){ this.futures = new Future<?>[capteur.observers.size()];
this.capteur.lock(); //lock le capteur pour que les autres puissent lire la même valeur que le premier int i = 0;
firstDone = true; for (ObserverDeCapteurAsync canal : capteur.observers) {
} Future<?> f = canal.update();
} this.futures[i] = f;
try { i++;
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
else{
for (Future<?> f : l) { //attend que les autres soient résolus boolean allDone = true;
try { for (Future<?> f : futures) {
f.get(); allDone = allDone && f.isDone();
} catch (InterruptedException | ExecutionException e) { }
e.printStackTrace(); if(allDone){
this.futures = null;
valueSave = false;
} }
} }
this.capteur.unlock2();
this.capteur.unlock();
} }
} }

View File

@@ -12,6 +12,6 @@ public class GetValueReq implements Callable<Integer> {
//AO2 - concrete MI //AO2 - concrete MI
public Integer call() { public Integer call() {
return capteur.getValue(); return capteur.getValueDiffusion();
} }
} }

View File

@@ -5,4 +5,6 @@ public interface AlgoDiffusion {
public void configure(); public void configure();
public void execute(); public void execute();
public int getValue();
} }

View File

@@ -5,4 +5,5 @@ public interface Capteur {
//AO2 - servant //AO2 - servant
public int getValue(); public int getValue();
public int getValueDiffusion();
} }

View File

@@ -17,7 +17,6 @@ public class main {
public static void main(String[] argv){ public static void main(String[] argv){
Scheduler scheduler = new Scheduler(NB_CANAUX); Scheduler scheduler = new Scheduler(NB_CANAUX);
CapteurImpl c = new CapteurImpl(); CapteurImpl c = new CapteurImpl();
c.setScheduler(scheduler.getScheculer());
DiffusionAtomique algoAtom = new DiffusionAtomique(); DiffusionAtomique algoAtom = new DiffusionAtomique();
algoAtom.capteur = c; algoAtom.capteur = c;

View File

@@ -36,7 +36,6 @@ public class TestAlgoDiffusionAtom {
Scheduler scheduler = new Scheduler(NB_CANAUX); Scheduler scheduler = new Scheduler(NB_CANAUX);
CapteurImpl c = new CapteurImpl(); CapteurImpl c = new CapteurImpl();
c.setScheduler(scheduler.getScheculer());
DiffusionAtomique algo = new DiffusionAtomique(); DiffusionAtomique algo = new DiffusionAtomique();
algo.capteur = c; algo.capteur = c;

View File

@@ -36,7 +36,6 @@ public class TestAlgoDiffusionEpoq {
Scheduler scheduler = new Scheduler(NB_CANAUX); Scheduler scheduler = new Scheduler(NB_CANAUX);
CapteurImpl c = new CapteurImpl(); CapteurImpl c = new CapteurImpl();
c.setScheduler(scheduler.getScheculer());
DiffusionEpoque algo = new DiffusionEpoque(); DiffusionEpoque algo = new DiffusionEpoque();
algo.capteur = c; algo.capteur = c;

View File

@@ -15,7 +15,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import impl.Afficheur; import impl.Afficheur;
import impl.Canal; import impl.Canal;
import impl.CapteurImpl; import impl.CapteurImpl;
import impl.DiffusionAtomique;
import impl.DiffusionSequencielle; import impl.DiffusionSequencielle;
import impl.Scheduler; import impl.Scheduler;
@@ -37,7 +36,6 @@ public class TestAlgoDiffusionSeq {
Scheduler scheduler = new Scheduler(NB_CANAUX); Scheduler scheduler = new Scheduler(NB_CANAUX);
CapteurImpl c = new CapteurImpl(); CapteurImpl c = new CapteurImpl();
c.setScheduler(scheduler.getScheculer());
DiffusionSequencielle algo = new DiffusionSequencielle(); DiffusionSequencielle algo = new DiffusionSequencielle();
algo.capteur = c; algo.capteur = c;