Advertisement
ridjis

Louvre

Dec 7th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.11 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import os.simulation.SimulationContainer;
  4. import os.simulation.SimulationContainerLayout;
  5. import os.simulation.SimulationThread;
  6. import os.simulation.gui.NoAnimationPanel;
  7. import os.simulation.gui.SimulationFrame;
  8. import os.simulation.gui.SimulationPanel;
  9. import os.simulation.gui.swing.SwingSimulationPanel;
  10. import java.util.concurrent.atomic.*;
  11.  
  12. public class Louvre {
  13.     private Obilasci o = new Obilasci();
  14.    
  15.     private class Obilasci {
  16.         AtomicInteger nemac = new AtomicInteger();
  17.         AtomicInteger italijan = new AtomicInteger();
  18.         AtomicInteger englez = new AtomicInteger();
  19.    
  20.         public void preNemac() {
  21.             boolean ok = true;
  22.             do {
  23.                 if (!ok)
  24.                     Thread.yield();
  25.                    
  26.                 int staraVr = nemac.get();
  27.                 int novaVr = staraVr + 1;
  28.                
  29.                 ok = (italijan.get() == 0) && (englez.get() == 0);
  30.                 if (ok)
  31.                     ok = nemac.compareAndSet(staraVr, novaVr);
  32.             } while (!ok);  
  33.         }
  34.        
  35.         public void posleNemac() {
  36.             nemac.decrementAndGet();
  37.         }
  38.        
  39.         public void preItalijan() {
  40.             boolean ok = true;
  41.             do {
  42.                 if (!ok)
  43.                     Thread.yield();
  44.                    
  45.                 int staraVr = italijan.get();
  46.                 int novaVr = staraVr + 1;
  47.                
  48.                 ok = (nemac.get() == 0) && (englez.get() == 0);
  49.                 if (ok)
  50.                     ok = italijan.compareAndSet(staraVr, novaVr);
  51.             } while (!ok);
  52.         }
  53.        
  54.         public void posleItalijan() {
  55.             italijan.decrementAndGet();
  56.         }
  57.        
  58.         public void preEnglez() {
  59.             boolean ok = true;
  60.             do {
  61.                 if (!ok)
  62.                     Thread.yield();
  63.                    
  64.                 int staraVr = englez.get();
  65.                 int novaVr = staraVr + 1;
  66.                
  67.                 ok = (italijan.get() == 0) && (nemac.get() == 0);
  68.                 if (ok)
  69.                     ok = englez.compareAndSet(staraVr, novaVr);
  70.             } while (!ok);
  71.         }
  72.  
  73.         public void posleEnglez() {
  74.             englez.decrementAndGet();
  75.         }
  76.    
  77.     }
  78.  
  79.     private class Englez extends UtilThread {
  80.  
  81.         public Englez(int id) {
  82.             super("\u0415\u043d\u0433\u043b\u0435\u0437 " + id, BOJA_ENGLEZ, odmaraEnglez);
  83.         }
  84.  
  85.         @Override
  86.         protected void step() {
  87.             odmara();
  88.             o.preEnglez();
  89.             obilazi();
  90.             o.posleEnglez();
  91.         }
  92.     }
  93.  
  94.     private class Nemac extends UtilThread {
  95.  
  96.         public Nemac(int id) {
  97.             super("\u041d\u0435\u043c\u0430\u0446 " + id, BOJA_NEMAC, odmaraNemac);
  98.         }
  99.  
  100.         @Override
  101.         protected void step() {
  102.             odmara();
  103.             o.preNemac();
  104.             obilazi();
  105.             o.posleNemac();
  106.         }
  107.     }
  108.  
  109.     private class Italijan extends UtilThread {
  110.  
  111.         public Italijan(int id) {
  112.             super("\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d " + id, BOJA_ITALIJAN, odmaraItalijan);
  113.         }
  114.  
  115.         @Override
  116.         protected void step() {
  117.             odmara();
  118.             o.preItalijan();
  119.             obilazi();
  120.             o.posleItalijan();
  121.         }
  122.     }
  123.  
  124.     // Parametri simulacije
  125.     public static final int    DUZINA_OBILASKA   = 3000;
  126.     public static final int    DUZINA_ODMARANJA  = 5000;
  127.     public static final int    BROJ_DJAKA        = 8;
  128.  
  129.     // Boje
  130.     public static final Color  BOJA_ENGLEZ        = new Color(0xFF, 0x44, 0x44);
  131.     public static final Color  BOJA_NEMAC         = new Color(0x22, 0x22, 0x22);
  132.     public static final Color  BOJA_ITALIJAN      = new Color(0x44, 0xCC, 0x44);
  133.    
  134.     public static final Color  BOJA_POZ_ENGLEZI   = new Color(0xFF, 0x44, 0x44);
  135.     public static final Color  BOJA_POZ_NEMCI     = new Color(0x44, 0x44, 0x44);
  136.     public static final Color  BOJA_POZ_ITALIJANI = new Color(0x44, 0xCC, 0x44);
  137.     public static final Color  BOJA_MUZEJA        = null;
  138.  
  139.     // Stringovi
  140.     public static final String TEXT_OBILAZI         = "\u041e\u0431\u0438\u043b\u0430\u0437\u0438";
  141.     public static final String TEXT_OBISAO          = "\u041e\u0431\u0438\u0448\u0430\u043e";
  142.     public static final String TEXT_ODMARA          = "\u041E\u0434\u043C\u0430\u0440\u0430";
  143.     public static final String TEXT_ODMORIO         = "\u0421\u043F\u0440\u0435\u043C\u0430\u043D";
  144.     public static final String TEXT_GRUPA_ENGLEZI   = "\u0413\u0440\u0443\u043f\u0430\u0020\u0415\u043d\u0433\u043b\u0435\u0437\u0430";
  145.     public static final String TEXT_GRUPA_NEMCI     = "\u0413\u0440\u0443\u043f\u0430\u0020\u041d\u0435\u043c\u0430\u0446\u0430";
  146.     public static final String TEXT_GRUPA_ITALIJANI = "\u0413\u0440\u0443\u043f\u0430\u0020\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d\u0430";
  147.     public static final String TEXT_MUZEJ           = "\u041c\u0443\u0437\u0435\u0458 \"Louvre\"";
  148.  
  149.     private class UtilThread extends SimulationThread {
  150.  
  151.         private SimulationContainer odmara;
  152.  
  153.         protected UtilThread(String name, Color color, SimulationContainer klupa) {
  154.             super(name, color);
  155.             odmara = klupa;
  156.         }
  157.  
  158.         protected void odmara() {
  159.             setContainer(odmara);
  160.             setText(TEXT_ODMARA);
  161.             work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
  162.             setText(TEXT_ODMORIO);
  163.         }
  164.  
  165.         protected void obilazi() {
  166.             setContainer(obilazi);
  167.             setText(TEXT_OBILAZI);
  168.             work(DUZINA_OBILASKA, 2 * DUZINA_OBILASKA);
  169.             setText(TEXT_OBISAO);
  170.         }
  171.     }
  172.  
  173.     // Glavni program
  174.     public static void main(String[] a) {
  175.         new Louvre();
  176.     }
  177.  
  178.     // Stanja
  179.     private SimulationContainer obilazi = new SimulationContainer(TEXT_MUZEJ, BOJA_MUZEJA, SimulationContainerLayout.BOX);
  180.     private SimulationContainer odmaraEnglez = new SimulationContainer(TEXT_GRUPA_ENGLEZI, BOJA_POZ_ENGLEZI, SimulationContainerLayout.BOX);
  181.     private SimulationContainer odmaraNemac = new SimulationContainer(TEXT_GRUPA_NEMCI, BOJA_POZ_NEMCI, SimulationContainerLayout.BOX);
  182.     private SimulationContainer odmaraItalijan = new SimulationContainer(TEXT_GRUPA_ITALIJANI, BOJA_POZ_ITALIJANI, SimulationContainerLayout.BOX);
  183.     private SimulationContainer stajaliste = new SimulationContainer(SimulationContainerLayout.ROW, odmaraNemac, odmaraItalijan, odmaraEnglez);
  184.     private SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, stajaliste, obilazi);
  185.  
  186.     public Louvre() {
  187.  
  188.         // Create frame
  189.         SimulationPanel panel = new SwingSimulationPanel(sve);
  190.         SimulationFrame frame = SimulationFrame.create("Louvre", panel, new NoAnimationPanel());
  191.         frame.display();
  192.  
  193.         // Create threads
  194.         for (int i = 1; i <= BROJ_DJAKA; i++) {
  195.             new Englez(i).start();
  196.             new Nemac(i).start();
  197.             new Italijan(i).start();
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement