test (à clean), essai de diffusion asynchrone qui utilise le scheluder dans CapteurImpl
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.concurrent.Future;
|
||||
|
||||
public class Afficheur implements ObserverdeCapteur {
|
||||
private int id= new Random().nextInt(100);
|
||||
public ArrayList<Integer> vals = new ArrayList();
|
||||
public ArrayList<Integer> vals = new ArrayList<Integer>(); //pour que les tests puissent lire les valeur après l'exécution
|
||||
|
||||
public Afficheur() {}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class Afficheur implements ObserverdeCapteur {
|
||||
try{
|
||||
Future<Integer> futureValue= capteurAsync.getValue();
|
||||
Integer value=futureValue.get();
|
||||
System.out.println("Afficheur " + id+", value: "+ value);
|
||||
System.out.println("\t\tAfficheur " + id+", value: "+ value);
|
||||
vals.add(value);
|
||||
}catch(Exception e){
|
||||
//TODO
|
||||
|
||||
@@ -6,12 +6,18 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
|
||||
public class CapteurImpl implements Capteur {
|
||||
private ScheduledExecutorService scheduler;
|
||||
private int value = 0;
|
||||
private AlgoDiffusion diffusion;
|
||||
protected List<ObserverDeCapteurAsync> observers=new ArrayList<ObserverDeCapteurAsync>();
|
||||
private Boolean locker = false;
|
||||
private Boolean locker2 = false;
|
||||
|
||||
private long initialTime = System.currentTimeMillis(); // debug
|
||||
|
||||
@Override
|
||||
public void attach(ObserverDeCapteurAsync o) {
|
||||
@@ -25,20 +31,32 @@ public class CapteurImpl implements Capteur {
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
System.out.println("\t\ttick");
|
||||
this.value++;
|
||||
System.out.println("tick call");
|
||||
if(!locker){
|
||||
diffusion.execute();
|
||||
this.value++;
|
||||
System.out.println("tick(" + this.value+")\t\t" + (System.currentTimeMillis()-initialTime) + "ms");
|
||||
//diffusion.execute();
|
||||
if(!locker2){
|
||||
scheduler.execute(() -> diffusion.execute());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void lock(){
|
||||
public synchronized void lock(){
|
||||
locker = true;
|
||||
}
|
||||
public void unlock(){
|
||||
public synchronized void unlock(){
|
||||
locker = false;
|
||||
}
|
||||
|
||||
public synchronized void lock2(){
|
||||
locker2 = true;
|
||||
}
|
||||
public synchronized void unlock2(){
|
||||
locker2 = false;
|
||||
}
|
||||
|
||||
|
||||
public void setAlgoDiffusion(AlgoDiffusion diffusion){
|
||||
this.diffusion = diffusion;
|
||||
}
|
||||
@@ -48,4 +66,11 @@ public class CapteurImpl implements Capteur {
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setScheduler(ScheduledExecutorService scheduler){
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
import interfaces.Capteur;
|
||||
import interfaces.ObserverDeCapteurAsync;
|
||||
|
||||
public class DiffusionAtomique implements AlgoDiffusion {
|
||||
@@ -21,7 +18,8 @@ public class DiffusionAtomique implements AlgoDiffusion {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
capteur.lock();
|
||||
this.capteur.lock();
|
||||
System.out.println("execute");
|
||||
Future<?>[] l = new Future<?>[capteur.observers.size()];
|
||||
int i = 0;
|
||||
for (ObserverDeCapteurAsync canal : capteur.observers) {
|
||||
@@ -36,6 +34,6 @@ public class DiffusionAtomique implements AlgoDiffusion {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
capteur.unlock();
|
||||
this.capteur.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
41
src/impl/DiffusionEpoque.java
Normal file
41
src/impl/DiffusionEpoque.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package impl;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
import interfaces.ObserverDeCapteurAsync;
|
||||
|
||||
public class DiffusionEpoque implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
}
|
||||
|
||||
@Override
|
||||
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++;
|
||||
}
|
||||
|
||||
for (Future<?> f : l) { //attend que les autres soient résolus //TODO
|
||||
try {
|
||||
f.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.capteur.unlock2();
|
||||
//this.capteur.unlock();
|
||||
}
|
||||
}
|
||||
56
src/impl/DiffusionSequencielle.java
Normal file
56
src/impl/DiffusionSequencielle.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package impl;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
import interfaces.ObserverDeCapteurAsync;
|
||||
|
||||
public class DiffusionSequencielle implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
for (Future<?> f : l) {
|
||||
if(f.isDone()){
|
||||
this.capteur.lock(); //lock le capteur pour que les autres puissent lire la même valeur que le premier
|
||||
firstDone = true;
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (Future<?> f : l) { //attend que les autres soient résolus
|
||||
try {
|
||||
f.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.capteur.unlock2();
|
||||
this.capteur.unlock();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ public class Scheduler {
|
||||
|
||||
public Scheduler(int nbCanaux) {
|
||||
int nThread = 20+2*nbCanaux;
|
||||
scheduler = Executors.newScheduledThreadPool(nThread);
|
||||
this.scheduler = Executors.newScheduledThreadPool(nThread);
|
||||
}
|
||||
|
||||
public ScheduledExecutorService getScheculer(){
|
||||
|
||||
Reference in New Issue
Block a user