Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) throws InterruptedException {
  6. Chiusa c = new Chiusa();
  7. int id = 0;
  8. while (true) {
  9. int provenienza = (int)Math.floor(Math.random()*3);
  10. int destinazione;
  11. do {
  12. destinazione = (int)Math.floor(Math.random()*3);
  13. } while(provenienza == destinazione);
  14. Thread.sleep(1000);
  15. new Barca(provenienza, destinazione, ++id, c);
  16. }
  17. }
  18. }
  19.  
  20. class Chiusa {
  21. private int livello;
  22. private int nBarche;
  23. private LinkedList<Barca> coda0;
  24. private LinkedList<Barca> coda1;
  25. private LinkedList<Barca> coda2;
  26. private boolean up;
  27.  
  28. public Chiusa() {
  29. this.livello = 0;
  30. this.nBarche = 0;
  31. this.up = true;
  32. this.coda0 = new LinkedList<>();
  33. this.coda1 = new LinkedList<>();
  34. this.coda2 = new LinkedList<>();
  35. }
  36.  
  37. public synchronized void accediAllaChiusa(Barca b) throws InterruptedException {
  38.  
  39. System.out.println(b+" vuole accedere alla chiusa. Suo livello: "+b.provenienza+ "chiusa: "+livello);
  40.  
  41. while (b.provenienza != livello) {
  42. System.out.println("La nave "+b+" non può accedere alla chiusa");
  43. wait();
  44. }
  45. System.out.println("La nave "+b+" accede alla chiusa");
  46.  
  47. if (b.destinazione == 0) {
  48. this.coda0.push(b);
  49. } else if (b.destinazione == 1) {
  50. this.coda1.push(b);
  51. } else {
  52. this.coda2.push(b);
  53. }
  54.  
  55. nBarche++;
  56. changeLevel();
  57. }
  58.  
  59. private void changeLevel() throws InterruptedException {
  60. if (nBarche != 4) {
  61. return;
  62. }
  63.  
  64. System.out.println("Cambio di livello... adesso è "+livello);
  65. System.out.println("##############");
  66. System.out.println("##############");
  67.  
  68. System.out.println("Le navi che devono andare a 0: "+coda0);
  69. System.out.println("Le navi che devono andare a 1: "+coda1);
  70. System.out.println("Le navi che devono andare a 2: "+coda2);
  71.  
  72. System.out.println("##############");
  73. System.out.println("##############");
  74.  
  75.  
  76. Thread.sleep(17000);
  77. if (up) livello++;
  78. else livello--;
  79.  
  80. if (livello == 2) up = false;
  81. if (livello == 0) up = true;
  82.  
  83.  
  84. System.out.println("Cambio effettuato, il livello è "+livello);
  85. esciDallaChiusa();
  86. notifyAll();
  87. }
  88.  
  89. private void esciDallaChiusa() {
  90. if (livello == 0) {
  91. System.out.println(this.coda0 + " escono dalla chiusa, livello 0");
  92. nBarche-=this.coda0.size();
  93. this.coda0.clear();
  94. } else if(livello == 1) {
  95. System.out.println(this.coda1 + " escono dalla chiusa, livello 1");
  96. nBarche-=this.coda1.size();
  97. this.coda1.clear();
  98. } else {
  99. System.out.println(this.coda2 + " escono dalla chiusa, livello 2");
  100. nBarche-=this.coda2.size();
  101. this.coda2.clear();
  102. }
  103.  
  104. }
  105. }
  106.  
  107. class Barca extends Thread{
  108. public int provenienza;
  109. public int destinazione;
  110. public int id;
  111. private Chiusa c;
  112. public Barca(int provenienza, int destinazione, int id, Chiusa c) {
  113. this.provenienza = provenienza;
  114. this.destinazione = destinazione;
  115. this.id = id;
  116. this.c = c;
  117. this.start();
  118. }
  119.  
  120. @Override
  121. public String toString() {
  122. String str = "BARCA-"+id+" FROM "+provenienza+" TO "+destinazione;
  123. return str;
  124. }
  125.  
  126. @Override
  127. public void run() {
  128. try {
  129. this.c.accediAllaChiusa(this);
  130. } catch (InterruptedException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement