public class Semaforo 
{
 private int valor ;
 Semaforo()
 {
 valor = 1; 
 }
 Semaforo(int p)
 { 
 valor = p; 
 }

 synchronized public void up() 
 {
 this.valor += 1; 
 }

 synchronized public void down()
 {
 while (valor <= 0)
  {
  try
   {
   wait();
   }
   catch (InterruptedException e)
   {
   }
  }
  if (valor > 0)
  valor -= 1;
 }

 public String toString()
 {
 return ( " " + this.valor ); 
 }

 public int get()
 { 
 return this.valor; 
 }
} 
/*
 public static void main(String [] args) throws InterruptedException
 {
 Semaforo S = new Semaforo(5);
 S.down ();
 S.down ();
 System.out.println (S);
 PingPong S = new PingPong ("Pong", 33, S);
 S2.start ();
 S2.stop ();
 S2.k.down ();
 System.out.println(S2.k);
 }
 }
 
 class PingPong extends Thread 
 {
 String palavra;
 int tempo; 
 Semaforo k;
 PingPong(String t, int w, Semaforo s)
 {
 palavra = t; tempo = w; k = s;}
 public void run()
 {
 try 
 {
 while (true) 
 {System.out.print(palavra + " ");
 sleep(tempo); 
 }
 }
 catch (InterruptedException e)
 {
 return;
 }
 }

 public static void main(String [] args) 
 { 
 Thread t1 = new PingPong("Ping",33);
 Thread t2 = new PingPong("Pong",100);
 t1.start(); t2.start(); 
}
}
*/