Gunior

Untitled

May 24th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.concurrent.Semaphore;
  2.  
  3.  
  4. public class Carro extends Thread {
  5.  
  6.     int Sentido;
  7.    
  8.    
  9.     private static Semaphore ponte = new Semaphore(1);
  10.  
  11.    
  12.     public void run() {
  13.         try {
  14.         ponte.acquire();
  15.        
  16.         if(Sentido == 0 ){
  17.             System.out.println(getName() + " Carro descendo");
  18.             sleep(1000);
  19.             System.out.println(getName() + " Ponte liberada");
  20.             ponte.release();
  21.         }
  22.        
  23.         else if(Sentido == 1){
  24.             System.out.println(getName() + " Carro subindo");
  25.             sleep((long)(Math.random() * 10000));
  26.             System.out.println(getName() + " Ponte liberada");
  27.             ponte.release();
  28.         }
  29.        
  30.         else{
  31.             System.out.println("thread erro run");
  32.         }
  33.        
  34.         }catch(InterruptedException ie){ ie.printStackTrace(); }
  35.        
  36.         }
  37.    
  38.    
  39.    
  40.     Carro(int i){
  41.         this.Sentido = i;
  42.     }
  43.    
  44.    
  45.     int getSentido(){
  46.         return Sentido;
  47.     }
  48.    
  49.     void setSentido(int Sentido){
  50.         this.Sentido = Sentido;
  51.     }
  52.    
  53.    
  54.    
  55.    
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment