atomique et epoque fini
This commit is contained in:
@@ -6,15 +6,13 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
|
||||
public class CapteurImpl implements Capteur {
|
||||
private int value = 0;
|
||||
private AlgoDiffusion diffusion;
|
||||
protected List<ObserverDeCapteurAsync> observers=new ArrayList<ObserverDeCapteurAsync>();
|
||||
private Boolean locker = false;
|
||||
private Boolean locker2 = false;
|
||||
private boolean lock = false; //utilisé uniquement par les test !
|
||||
|
||||
private long initialTime = System.currentTimeMillis(); // debug
|
||||
|
||||
@@ -23,35 +21,19 @@ public class CapteurImpl implements Capteur {
|
||||
try {
|
||||
observers.add(o);
|
||||
}catch(Exception e) {
|
||||
//TODO
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
if(!locker){
|
||||
public void tick() {
|
||||
if(!lock){ //utilisé uniqueemnt par les test pour bloquer la maj du capteur
|
||||
this.value++;
|
||||
System.out.println("tick(" + this.value+")\t\t" + (System.currentTimeMillis()-initialTime) + "ms");
|
||||
}
|
||||
System.out.println("tick(" + this.value+")\t\t" + (System.currentTimeMillis()-initialTime) + "ms" + " locker : " + locker);
|
||||
diffusion.execute();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public synchronized void lock(){
|
||||
locker = true;
|
||||
}
|
||||
public synchronized void unlock(){
|
||||
locker = false;
|
||||
}
|
||||
|
||||
public synchronized void lock2(){
|
||||
locker2 = true;
|
||||
}
|
||||
public synchronized void unlock2(){
|
||||
locker2 = false;
|
||||
}
|
||||
|
||||
|
||||
public void setAlgoDiffusion(AlgoDiffusion diffusion){
|
||||
@@ -70,4 +52,12 @@ public class CapteurImpl implements Capteur {
|
||||
}
|
||||
|
||||
|
||||
public void stop(){
|
||||
this.lock = true;
|
||||
}
|
||||
|
||||
public boolean isStop(){
|
||||
return this.lock;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package impl;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
@@ -9,6 +10,8 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
public class DiffusionAtomique implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
private Future<?>[] futures;
|
||||
private Queue<Integer> values = new LinkedList<>();
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
@@ -16,24 +19,40 @@ public class DiffusionAtomique implements AlgoDiffusion {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValue() {
|
||||
return this.values.peek();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
this.capteur.lock();
|
||||
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++;
|
||||
|
||||
if(!this.capteur.isStop()){
|
||||
this.values.offer(this.capteur.getValue());
|
||||
}
|
||||
for (Future<?> f : l) {
|
||||
try {
|
||||
f.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
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;
|
||||
this.values.remove();
|
||||
System.out.println("next value");
|
||||
}
|
||||
}
|
||||
this.capteur.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,33 +9,27 @@ import interfaces.ObserverDeCapteurAsync;
|
||||
public class DiffusionEpoque implements AlgoDiffusion {
|
||||
|
||||
public CapteurImpl capteur;
|
||||
private Future<?>[] futures;
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'configure'");
|
||||
this.futures = new Future<?>[capteur.observers.size()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValue() {
|
||||
return this.capteur.getValue();
|
||||
}
|
||||
|
||||
@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;
|
||||
if(futures[i]==null || futures[i].isDone()){
|
||||
futures[i] = canal.update();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package impl;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import interfaces.AlgoDiffusion;
|
||||
@@ -31,7 +30,7 @@ public class DiffusionSequencielle implements AlgoDiffusion {
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
if(futures == null){
|
||||
if(this.futures == null){
|
||||
System.out.println("execute");
|
||||
this.futures = new Future<?>[capteur.observers.size()];
|
||||
int i = 0;
|
||||
|
||||
@@ -54,9 +54,13 @@ public class TestAlgoDiffusionAtom {
|
||||
|
||||
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--){
|
||||
System.out.println("restant " + i + "s");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
future.cancel(false);
|
||||
Thread.sleep(3100); //3000ms max + 100ms de marge
|
||||
|
||||
clock.shutdown();
|
||||
//Thread.sleep(1500);
|
||||
|
||||
@@ -48,6 +48,8 @@ public class TestAlgoDiffusionEpoq {
|
||||
Canal canal = new Canal(c, scheduler, afficheur);
|
||||
c.attach(canal);
|
||||
}
|
||||
|
||||
algo.configure();
|
||||
|
||||
ScheduledExecutorService clock = scheduler.getScheculer();
|
||||
ScheduledFuture<?> future = clock.scheduleAtFixedRate(() -> c.tick(), 0, 500, TimeUnit.MILLISECONDS);
|
||||
|
||||
Reference in New Issue
Block a user