Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- Chiusa c = new Chiusa();
- int id = 0;
- while (true) {
- int provenienza = (int)Math.floor(Math.random()*3);
- int destinazione;
- do {
- destinazione = (int)Math.floor(Math.random()*3);
- } while(provenienza == destinazione);
- Thread.sleep(1000);
- new Barca(provenienza, destinazione, ++id, c);
- }
- }
- }
- class Chiusa {
- private int livello;
- private int nBarche;
- private LinkedList<Barca> coda0;
- private LinkedList<Barca> coda1;
- private LinkedList<Barca> coda2;
- private boolean up;
- public Chiusa() {
- this.livello = 0;
- this.nBarche = 0;
- this.up = true;
- this.coda0 = new LinkedList<>();
- this.coda1 = new LinkedList<>();
- this.coda2 = new LinkedList<>();
- }
- public synchronized void accediAllaChiusa(Barca b) throws InterruptedException {
- System.out.println(b+" vuole accedere alla chiusa. Suo livello: "+b.provenienza+ "chiusa: "+livello);
- while (b.provenienza != livello) {
- System.out.println("La nave "+b+" non può accedere alla chiusa");
- wait();
- }
- System.out.println("La nave "+b+" accede alla chiusa");
- if (b.destinazione == 0) {
- this.coda0.push(b);
- } else if (b.destinazione == 1) {
- this.coda1.push(b);
- } else {
- this.coda2.push(b);
- }
- nBarche++;
- changeLevel();
- }
- private void changeLevel() throws InterruptedException {
- if (nBarche != 4) {
- return;
- }
- System.out.println("Cambio di livello... adesso è "+livello);
- System.out.println("##############");
- System.out.println("##############");
- System.out.println("Le navi che devono andare a 0: "+coda0);
- System.out.println("Le navi che devono andare a 1: "+coda1);
- System.out.println("Le navi che devono andare a 2: "+coda2);
- System.out.println("##############");
- System.out.println("##############");
- Thread.sleep(17000);
- if (up) livello++;
- else livello--;
- if (livello == 2) up = false;
- if (livello == 0) up = true;
- System.out.println("Cambio effettuato, il livello è "+livello);
- esciDallaChiusa();
- notifyAll();
- }
- private void esciDallaChiusa() {
- if (livello == 0) {
- System.out.println(this.coda0 + " escono dalla chiusa, livello 0");
- nBarche-=this.coda0.size();
- this.coda0.clear();
- } else if(livello == 1) {
- System.out.println(this.coda1 + " escono dalla chiusa, livello 1");
- nBarche-=this.coda1.size();
- this.coda1.clear();
- } else {
- System.out.println(this.coda2 + " escono dalla chiusa, livello 2");
- nBarche-=this.coda2.size();
- this.coda2.clear();
- }
- }
- }
- class Barca extends Thread{
- public int provenienza;
- public int destinazione;
- public int id;
- private Chiusa c;
- public Barca(int provenienza, int destinazione, int id, Chiusa c) {
- this.provenienza = provenienza;
- this.destinazione = destinazione;
- this.id = id;
- this.c = c;
- this.start();
- }
- @Override
- public String toString() {
- String str = "BARCA-"+id+" FROM "+provenienza+" TO "+destinazione;
- return str;
- }
- @Override
- public void run() {
- try {
- this.c.accediAllaChiusa(this);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement