Programacion Concurrente
  Baño semaforos
 

import java.io.*;

class Semaforo{

 final static int DISPONIBLE = 1;
 final static int LLENO = 0;
 protected int estado = DISPONIBLE;
 protected int chicos_dentro=0;
 protected int chicas_dentro=0;
 protected int wait_chicos=0;
 protected int wait_chicas=0;
 protected int lugares=0;

public synchronized void Chicas(){
 while(estado==LLENO){
  try{
   wait_chicos++;
   wait();
     }catch(InterruptedException e){System.out.println(e.toString());}
 }
 
 if(wait_chicos>0) wait_chicos--;
 lugares++;
 chicos_dentro++;
 if(lugares<5)estado = DISPONIBLE;
 else estado = LLENO;
}

public synchronized void Chicos(){
 while((estado==LLENO)||(chicos_dentro>0)||(wait_chicos>0)){
 try{
  wait_chicas++;
  wait();
  }catch(InterruptedException e){System.out.println(e.toString());
 }

 if(wait_chicos==0&&chicas_dentro==0&&chicos_dentro==0){
 lugares++;
 chicas_dentro++;
 wait_chicas--;
 estado=LLENO;
 }
estado=LLENO;
}
}
public synchronized void dsblq_chicos(){
 chicos_dentro--;
 estado=DISPONIBLE;
 lugares--;
 notifyAll();
}

public synchronized void dsblq_chicas(){
 chicas_dentro--;
 estado=DISPONIBLE;
 lugares--;
 notifyAll();
}

}

 
   
 
Este sitio web fue creado de forma gratuita con PaginaWebGratis.es. ¿Quieres también tu sitio web propio?
Registrarse gratis