Programacion Concurrente
  Parque
 

import java.io.*;

class term extends MyObject{
 private boolean lleno;
 private cola tail;
 private ConditionVariable salir = null;
 private ConditionVariabale llegar = null;
 private int numPasajeros;
 
 public term(){
  lleno = false;
  tail = new cola();
  salir = new ConditionVariable(); 
  llegar = new ConditionVariable();
  numPasajeros = 0;
 }
 
 
 public synchronized void salida(int n){
  numPasajeros = n;
  while(!lleno) wait(salir);
  tail.vaciar();
  System.out.println("tEl camion salio con "+numPasajeros+" personasn");
  //nap(200);
  System.out.println("tRegreso el autobusn ");
  lleno = false;
  numPasajeros = 0;
  notify(llegar);
 }
 
 public synchronized void llegada(Integer o){
  tail.ponDato(o);
  //nap(20);
  System.out.println("El pasajero "+o.intValue()+" abordo");
  while(tail.size==numPasajeros){
   lleno = true;
   notify(salir);
   wait(llegar); 
  }
 }
}
 
class llegando extends Thread{
 private term lleg;
 public llegando(term x) {this.lleg = x;}
 private int i = 1;
 public void run(){
  while(true)
  {
     lleg.llegada(new Integer(i));
     i++;
  }
  }
}
 
class saliendo extends Thread{
 private term sal;
 public saliendo(term x) {this.sal = x;}
 int i=1;
 
 public void run(){
  int numPasajeros;
  
  while(i<=5){
   do{
     numPasajeros = ( (int)(Math.random()*30) );    
   }while(numPasajeros <10 || numPasajeros>30);
   //System.out.println("----------"+numPasajeros);
   
   sal.salida(numPasajeros);
   i++;
   /*try {sleep(numPasajeros*1000);}
   catch(InterruptedException e){
    System.out.println("Error:"+e.toString());}
   System.out.println("Regreso el autobus -----------");*/
  }
 }
}

class Terminal extends MyObject{
 public static void main(String[] args){
  term ter = new term();
  llegando ll = new llegando(ter);
  saliendo sa = new saliendo(ter);
  ll.start();
  sa.start();
  nap(2000);
        System.exit(0);
 }
}
   
       
    

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