Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.Thread;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.Condition;
- public class PonteSemSinal {
- private Car carro;
- private Caminhao truck;
- private Bridge ponte;
- private Random opt = new Random(); //variável que sorteia a direção a ser criada para carros. Ao gerar 0=left, 1=right;
- private Random opt2 = new Random();
- private List<Car> carrosEsquerda = new ArrayList<>();
- private List<Car> carrosDireita = new ArrayList<>();
- private List<Caminhao> caminhoesEsquerda = new ArrayList<>();
- private List<Caminhao> caminhoesDireita = new ArrayList<>();
- private int direction;
- private int carsDir, carsEsq, camDir, camEsq;
- private int turn;
- private boolean waiting = false;
- public PonteSemSinal () {
- ponte = new Bridge();
- this.carsDir = 50;
- this.carsEsq = 50;
- this.camDir = 3;
- this.camEsq = 3;
- }
- public void getSituation (int s) {}
- public synchronized void await() throws InterruptedException {
- while(!waiting) wait();
- try {
- Random choose = new Random();
- Random dir = new Random();
- turn = choose.nextInt(2); // Criar carro ou caminhão aleatoriamente.
- if ((turn == 0) && ((camDir != 0) || (camEsq != 0))) {
- if ((camDir == 0) && (camEsq != 0)) {
- direction = 0;
- truck = new Caminhao(ponte, 0);
- truck.setOp(3);
- truck.setTime();
- System.out.println(truck.toString());
- caminhoesEsquerda.add(truck);
- }
- if ((camEsq == 0) && (camDir != 0)) {
- direction = 1;
- truck = new Caminhao(ponte, 1);
- truck.setOp(3);
- truck.setTime();
- System.out.println(truck.toString());
- caminhoesDireita.add(truck);
- } else {
- direction = dir.nextInt(2);
- truck = new Caminhao(ponte, direction);
- truck.setOp(3);
- truck.setTime();
- System.out.println(truck.toString());
- if (direction == 1) {
- caminhoesDireita.add(truck);
- } else {
- caminhoesEsquerda.add(truck);
- }
- }
- }
- if ((turn == 1) && ((carsDir != 0) || (carsEsq != 0))) {
- if ((carsDir == 0) && (carsEsq != 0)) {
- direction = 0;
- carro = new Car(ponte, 0);
- carro.setOp(3);
- carro.setTime();
- System.out.println(carro.toString());
- carrosEsquerda.add(carro);
- }
- if ((carsEsq == 0) && (carsDir != 0)) {
- direction = 1;
- carro = new Car(ponte, 0);
- carro.setOp(3);
- carro.setTime();
- System.out.println(carro.toString());
- carrosDireita.add(carro);
- } else {
- direction = dir.nextInt(2);
- carro = new Car(ponte, direction);
- carro.setOp(3);
- carro.setTime();
- System.out.println(carro.toString());
- if (direction == 1) {
- carrosDireita.add(carro);
- } else {
- carrosEsquerda.add(carro);
- }
- }
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void signal() {
- try {
- if ((turn == 0) && (direction == 0)) {
- long ta = caminhoesEsquerda.get(3 - camEsq).getTime();
- TimeUnit.SECONDS.sleep(ta);
- ponte.atravessaCam(caminhoesEsquerda.get(3 - camEsq), 3);
- camEsq--;
- }
- if ((turn == 0) && (direction == 1)) {
- long ta = caminhoesDireita.get(3 - camDir).getTime();
- TimeUnit.SECONDS.sleep(ta);
- ponte.atravessaCam(caminhoesDireita.get(3 - camDir), 3);
- camDir--;
- }
- if ((turn == 1) && (direction == 0)) {
- TimeUnit.SECONDS.sleep(2);
- long ta = carrosEsquerda.get(50 - carsEsq).getTime();
- TimeUnit.SECONDS.sleep(ta);
- ponte.atravessaCar(carrosEsquerda.get(50 - carsEsq), 3);
- carsEsq--;
- }
- if ((turn == 1) && (direction == 1)) {
- TimeUnit.SECONDS.sleep(2);
- long ta = carrosEsquerda.get(50 - carsDir).getTime();
- TimeUnit.SECONDS.sleep(ta);
- ponte.atravessaCar(carrosEsquerda.get(50 - carsDir), 3);
- carsDir--;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- waiting=true;
- notifyAll();
- }
- }
Add Comment
Please, Sign In to add comment