public class Lector extends Comun implements Runnable {
private int i;
public Lector (int _i) {
i=_i;
}
public void run () {
for (int j=0; j<3; j++) {
System.out.println ("Lector "+i+" quiere leer");
mutex.WAIT();
lectores++;
if (lectores == 1)
escritores.WAIT();
mutex.SIGNAL();
bd.leer (i);
mutex.WAIT();
lectores--;
if (lectores == 0)
escritores.SIGNAL();
mutex.SIGNAL();
System.out.println ("Lector "+i+" usa su dato");
}
}
}
public class Escritor extends Comun implements Runnable {
private int i;
public Escritor (int _i) {
i=_i;
}
public void run () {
for (int j=0;j<3;j++) {
System.out.println ("Escritor "+i+" quiere escribir");
escritores.WAIT();
bd.escribir (i,i);
escritores.SIGNAL();
}
}
}
public class SemLectoresEscritores {
public static void main (String args[]) {
for (int i=0;i<5;i++)
new Thread (new Escritor (i)).start();
for (int i=0;i<5;i++)
new Thread (new Lector (i)).start();
}
}
Ejecucion del programa:

|