コード例 #1
0
 public void run() {
   try {
     while (true) {
       monArb.entrarLeer(currentThread());
       monArb.entrarLeer(currentThread());
       Thread.sleep(3500);
       if (Math.random() < 0.5) Thread.sleep(2500);
       System.out.println("Reader " + currentThread().getId() + " reads " + baseDatos.leer());
       monArb.salirLeer(currentThread());
     }
   } catch (InterruptedException e) {
   }
 }
コード例 #2
0
  public void run() {
    try {
      while (true) {
        monArb.entrarEscribir(miNum);
        Thread.sleep(3500);
        if (Math.random() < 0.5) Thread.sleep(2500);

        // Posibilidad de que vuelva a reentrar
        if (Math.random() > 0.5) {
          monArb.entrarEscribir(miNum);
          Thread.sleep(2500);
          if (Math.random() < 0.5) Thread.sleep(1500);
          baseDatos.escribir(miNum);
          System.out.println("Escritor " + miNum + " escribe su numero");
          monArb.salirEscribir(miNum);
        }
        baseDatos.escribir(miNum);
        System.out.println("Escritor " + miNum + " escribe su numero");
        monArb.salirEscribir(miNum);
      }
    } catch (InterruptedException e) {
    }
  }