Advertisement
Dzok1517

AtomiciProizPotr

Jan 11th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. package djolepokusavaatomike;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.atomic.AtomicBoolean;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6.  
  7. import os.simulation.Application;
  8. import os.simulation.AutoCreate;
  9. import os.simulation.Color;
  10. import os.simulation.Container;
  11. import os.simulation.Item;
  12. import os.simulation.Operation;
  13. import os.simulation.Thread;
  14.  
  15. /*
  16.  * Dat je bafer fiksne velicine. Vise procesa zeli da istovremeno dodaje i
  17.  * uklanja elemente sa ovog bafera.
  18.  *
  19.  * Realizovati operaciju dodavanja tako da, ako je bafer pun, blokira proces
  20.  * dok se ne oslobodi mesto za novi element. Takodje, realizovati operaciju
  21.  * uklanjanja tako da, ako je bafer prazam, blokira proces dok se ne doda novi
  22.  * element.
  23.  */
  24. public class BaferAtomici extends Application{
  25.    
  26.  
  27.     protected Bafer bafer = new Bafer(12);
  28.     protected class Bafer {
  29.        
  30.         private AtomicInteger brEle = new AtomicInteger(0);
  31.         private AtomicInteger brMesta = new AtomicInteger(12); // vidi
  32.         private AtomicBoolean ko = new AtomicBoolean(true);
  33.        
  34.  
  35.         private final List<Element> lista = new ArrayList<>();
  36.         private final int velicina;
  37.  
  38.         public Bafer(int velicina) {
  39.             this.velicina = velicina;
  40.         }
  41.         // Sinhronizacija
  42.         public void stavi(Element o) {
  43.            
  44.             boolean ok;
  45.             do {
  46.                 int oldValue = brMesta.get();
  47.                 int newValue = oldValue - 1;
  48.                 ok = newValue >= 0 ;
  49.                 if(ok) {
  50.                     ok = brMesta.compareAndSet(oldValue, newValue);
  51.                 }  
  52.             }while(!ok);
  53.                        
  54.             while(!ko.compareAndSet(true, false)) {}
  55.             try {
  56.                        
  57.                 lista.add(o);
  58.                 elementi.addItem(o);
  59.             }finally {
  60.                 ko.set(true);
  61.             }
  62.             brEle.incrementAndGet();
  63.         }
  64.  
  65.         // Sinhronizacija
  66.         public Element uzmi() {
  67.            
  68.             boolean ok;
  69.             do {
  70.                 int oldValue = brEle.get();
  71.                 int newValue = oldValue - 1 ;
  72.                 ok = newValue >=0 ;
  73.                 if(ok) {
  74.                     ok = brEle.compareAndSet(oldValue, newValue);
  75.                 }  
  76.             }while(!ok);
  77.            
  78.             while(!ko.compareAndSet(true, false)) {}
  79.             Element result;
  80.             try {
  81.                            
  82.                 result = lista.remove(0);
  83.                 elementi.removeItem(result);
  84.                 brMesta.incrementAndGet();
  85.                 return result;
  86.                
  87.             }finally {
  88.                 ko.set(true);
  89.  
  90.         }
  91.             }
  92.     }
  93.  
  94.     // ------------------- //
  95.     //    Sistemski deo    //
  96.     // ------------------- //
  97.     // Ne dirati kod ispod //
  98.     // ------------------- //
  99.  
  100.     @AutoCreate(4)
  101.     protected class Proizvodjac extends Thread {
  102.  
  103.         private final int id = getID();
  104.         private int br = 0;
  105.  
  106.         @Override
  107.         protected void step() {
  108.             Element element = proizvedi(id + "x" + (br++));
  109.             bafer.stavi(element);
  110.         }
  111.  
  112.     }
  113.  
  114.     @AutoCreate(4)
  115.     protected class Potrosac extends Thread {
  116.  
  117.         @Override
  118.         protected void step() {
  119.             Element element = bafer.uzmi();
  120.             potrosi(element);
  121.         }
  122.     }
  123.  
  124.     protected final Container proizvodjaci = box("Произвођачи").color(NAVY);
  125.     protected final Container potrosaci    = box("Потрошачи").color(MAROON);
  126.     protected final Container elementi     = box("Елементи").color(NEUTRAL_GRAY);
  127.     protected final Container main         = row(proizvodjaci, elementi, potrosaci);
  128.     protected final Operation proizvodjac  = init().name("Произв. %d").color(AZURE).text("Чека").container(proizvodjaci);
  129.     protected final Operation potrosac     = init().name("Потр. %d").color(ROSE).text("Чека").container(potrosaci);
  130.     protected final Operation element      = init();
  131.     protected final Operation proizvodnja  = duration("3±1").text("Производи").textAfter("Чека");
  132.     protected final Operation potrosnja    = duration("7±2").text("Троши %s").textAfter("Чека");
  133.  
  134.     protected Element proizvedi(String vrednost) {
  135.         proizvodnja.performUninterruptibly();
  136.         return new Element(vrednost);
  137.     }
  138.  
  139.     protected void potrosi(Element element) {
  140.         potrosnja.performUninterruptibly(element.getName());
  141.     }
  142.  
  143.     protected class Element extends Item {
  144.  
  145.         public Element(String vrednost) {
  146.             setName(vrednost);
  147.         }
  148.  
  149.         private int getIndex() {
  150.             return bafer.lista.indexOf(this);
  151.         }
  152.  
  153.         @Override
  154.         public Color getColor() {
  155.             int index = getIndex();
  156.             if ((index >= 0) && (index < bafer.velicina)) {
  157.                 return CHARTREUSE;
  158.             } else {
  159.                 return ORANGE;
  160.             }
  161.         }
  162.  
  163.         @Override
  164.         public String getText() {
  165.             return String.format("Bafer[%d]", getIndex());
  166.         }
  167.     }
  168.  
  169.     public static void main(String[] arguments) {
  170.         launch("Произвођачи и потрошачи");
  171.     }
  172.    
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement