Dzok1517

AtomiciBerberi

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