Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Semaphore;
- public class Carro extends Thread {
- int Sentido;
- private static Semaphore ponte = new Semaphore(1);
- public void run() {
- try {
- ponte.acquire();
- if(Sentido == 0 ){
- System.out.println(getName() + " Carro descendo");
- sleep(1000);
- System.out.println(getName() + " Ponte liberada");
- ponte.release();
- }
- else if(Sentido == 1){
- System.out.println(getName() + " Carro subindo");
- sleep((long)(Math.random() * 10000));
- System.out.println(getName() + " Ponte liberada");
- ponte.release();
- }
- else{
- System.out.println("thread erro run");
- }
- }catch(InterruptedException ie){ ie.printStackTrace(); }
- }
- Carro(int i){
- this.Sentido = i;
- }
- int getSentido(){
- return Sentido;
- }
- void setSentido(int Sentido){
- this.Sentido = Sentido;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment