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