70 lines
1.8 KiB
Java
70 lines
1.8 KiB
Java
|
// Time-stamp: <08 Apr 2008 11:35 queinnec@enseeiht.fr>
|
||
|
|
||
|
import java.util.concurrent.locks.Condition;
|
||
|
import java.util.concurrent.locks.Lock;
|
||
|
import java.util.concurrent.locks.ReentrantLock;
|
||
|
|
||
|
/**
|
||
|
* Lecteurs/rédacteurs stratégie d'ordonnancement: priorité aux rédacteurs,
|
||
|
* implantation: avec un moniteur.
|
||
|
*/
|
||
|
public class LectRed_PrioRedacteur implements LectRed {
|
||
|
|
||
|
Lock lock;
|
||
|
Condition requestRead;
|
||
|
Condition requestWrite;
|
||
|
boolean activeWriter;
|
||
|
int activeReaders;
|
||
|
int waitingWriters;
|
||
|
|
||
|
public LectRed_PrioRedacteur() {
|
||
|
lock = new ReentrantLock();
|
||
|
requestRead = lock.newCondition();
|
||
|
requestWrite = lock.newCondition();
|
||
|
activeWriter = false;
|
||
|
activeReaders = 0;
|
||
|
waitingWriters = 0;
|
||
|
}
|
||
|
|
||
|
public void demanderLecture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
while (activeWriter || waitingWriters > 0) {
|
||
|
requestRead.await();
|
||
|
}
|
||
|
activeReaders++;
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void terminerLecture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
activeReaders--;
|
||
|
if (activeReaders == 0) {
|
||
|
requestWrite.signal();
|
||
|
}
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void demanderEcriture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
waitingWriters++;
|
||
|
while (activeWriter || activeReaders > 0) {
|
||
|
requestWrite.await();
|
||
|
}
|
||
|
waitingWriters--;
|
||
|
activeWriter = true;
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void terminerEcriture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
activeWriter = false;
|
||
|
requestWrite.signalAll();
|
||
|
requestRead.signalAll();
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public String nomStrategie() {
|
||
|
return "Stratégie: Moniteur, prio rédacteur.";
|
||
|
}
|
||
|
}
|