Advertisement
Dzok1517

Untitled

Jan 9th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package solutions.locks;
  2.  
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. import os.simulation.Application;
  8. import os.simulation.AutoCreate;
  9. import os.simulation.Container;
  10. import os.simulation.Item;
  11. import os.simulation.Operation;
  12. import os.simulation.Thread;
  13.  
  14. /*
  15. * U frizerskom salonu rade dva berberina. Ako nema musterija, berber sedi u
  16. * svojoj stolici i spava. Kada musterija udje, ako neki od berbera spava, budi
  17. * ga, seda za stolicu i berber je sisa. Ako su svi berberi zauzeti, musterija
  18. * seda za stolicu u cekaonici i ceka da se oslobodi neko od berbera. Kada
  19. * berber zavrsi sisanje musterije, ako ima musterija koje cekaju, krece da
  20. * sisa jednu od musterija koje cekaju. Ako nema vise musterija koje cekaju,
  21. * berber seda u svoju stolicu i spava.
  22. *
  23. * Implementirati sinhronizaciju ove dve vrste procesa kako je opisano.
  24. */
  25.  
  26. public class UspavaniBerberiBrave extends Application{
  27.  
  28. private class Pristup{
  29. private Lock lock = new ReentrantLock() ;
  30. private Condition uslovZaBerbere = lock.newCondition();
  31. private Condition uslovZaMusterije = lock.newCondition();
  32.  
  33. private int brojacBerbera = 0 ;
  34. private int brojacMusterija = 0 ;
  35.  
  36. private void sacekajBerbera() throws InterruptedException {
  37. lock.lock();
  38. try {
  39. while(brojacBerbera == 0 )
  40. uslovZaMusterije.await();
  41.  
  42. brojacMusterija++;
  43. brojacBerbera--;
  44. uslovZaBerbere.signal();
  45.  
  46.  
  47. }finally {
  48. lock.unlock();
  49. }
  50. }
  51.  
  52. private void sacekajMusteriju() throws InterruptedException {
  53. lock.lock();
  54. try {
  55.  
  56. brojacBerbera++;
  57. uslovZaMusterije.signal();
  58.  
  59. while(brojacMusterija == 0)
  60. uslovZaBerbere.await();
  61.  
  62. brojacMusterija--;
  63.  
  64.  
  65. }finally {
  66. lock.unlock();
  67. }
  68. }
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. private Pristup pristup = new Pristup();
  76.  
  77. @AutoCreate(2)
  78. protected class Berber extends Thread {
  79.  
  80. @Override
  81. protected void step() {
  82. try {
  83. pristup.sacekajMusteriju();
  84. sisa();
  85.  
  86. } catch (InterruptedException e) {
  87.  
  88. }
  89.  
  90. }
  91. }
  92.  
  93. @AutoCreate
  94. protected class Musterija extends Thread {
  95.  
  96. @Override
  97. protected void run() {
  98. try {
  99. pristup.sacekajBerbera();
  100. sisaSe();
  101.  
  102. } catch (InterruptedException e) {
  103.  
  104. }
  105.  
  106. }
  107. }
  108.  
  109. // ------------------- //
  110. // Sistemski deo //
  111. // ------------------- //
  112. // Ne dirati kod ispod //
  113. // ------------------- //
  114.  
  115. protected final Container cekaonica = box("Чекаоница");
  116. protected final Container stolice = box("Салон");
  117. protected final Container main = column(cekaonica, stolice);
  118. protected final Operation berber = init().name("Бербер %d").color(ROSE).text("Спава").container(stolice).update(this::azuriraj);
  119. protected final Operation musterija = duration("1±1").name("Мушт. %d").color(AZURE).text("Чека").container(cekaonica).update(this::azuriraj);
  120. protected final Operation sisanjeB = duration("7").text("Шиша").update(this::azuriraj);
  121. protected final Operation sisanjeM = duration("7").text("Шиша се").container(stolice).colorAfter(CHARTREUSE).textAfter("Ошишао се").update(this::azuriraj);
  122.  
  123. protected void sisa() {
  124. sisanjeB.performUninterruptibly();
  125. }
  126.  
  127. protected void sisaSe() {
  128. sisanjeM.performUninterruptibly();
  129. }
  130.  
  131. protected void azuriraj(Item item) {
  132. long brB1 = 0;
  133. long brB2 = 0;
  134. for (Berber t : stolice.getItems(Berber.class)) {
  135. if (sisanjeB.getTextBefore().equals(t.getText())) {
  136. brB1++;
  137. } else {
  138. brB2++;
  139. }
  140. }
  141. long brM1 = stolice.stream(Musterija.class).count();
  142. long brM2 = cekaonica.stream(Musterija.class).count();
  143. cekaonica.setText(String.format("%d", brM2));
  144. stolice.setText(String.format("%d : %d", brB1, brM1));
  145. long razlika = brB1 - brM1;
  146. if (brB2 > 0 && brM2 > 0) {
  147. cekaonica.setColor(MAROON);
  148. } else {
  149. cekaonica.setColor(OLIVE);
  150. }
  151. if (razlika == 0) {
  152. stolice.setColor(ARMY);
  153. } else {
  154. stolice.setColor(MAROON);
  155. }
  156. }
  157.  
  158. @Override
  159. protected void initialize() {
  160. azuriraj(null);
  161. }
  162.  
  163. public static void main(String[] arguments) {
  164. launch("Успавани бербери");
  165. }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement