Advertisement
ridjis

LouvreLock

Dec 21st, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. package vezbe5semaphore;
  2.  
  3. import java.awt.Color;
  4. import java.util.concurrent.locks.Condition;
  5. import java.util.concurrent.locks.Lock;
  6. import java.util.concurrent.locks.ReentrantLock;
  7.  
  8. import os.simulation.SimulationContainer;
  9. import os.simulation.SimulationContainerLayout;
  10. import os.simulation.SimulationThread;
  11. import os.simulation.gui.NoAnimationPanel;
  12. import os.simulation.gui.SimulationFrame;
  13. import os.simulation.gui.SimulationPanel;
  14. import os.simulation.gui.swing.SwingSimulationPanel;
  15.  
  16. public class LouvreLock {
  17.     public final int BROJ_NACIJA = 3;
  18.     private Muzej m = new Muzej();
  19.    
  20.     private class Muzej {
  21.         Lock lock = new ReentrantLock();
  22.         Condition c = lock.newCondition();
  23.         int[] nacija = new int[BROJ_NACIJA];
  24.        
  25.         public Muzej() {
  26.             for (int i = 0; i < nacija.length; i++) {
  27.                 nacija[i] = 0;
  28.             }
  29.         }
  30.        
  31.         public void ulaz(int i) {
  32.             lock.lock();
  33.             try {
  34.                 while (nacija[(i+1) % nacija.length] > 0 || nacija[(i+2) % nacija.length] > 0) {
  35.                     c.awaitUninterruptibly();
  36.                 }
  37.                 nacija[i]++;
  38.             } finally { lock.unlock(); }
  39.         }
  40.        
  41.         public void izlaz(int i) {
  42.             lock.lock();
  43.             try {
  44.                 nacija[i]--;
  45.                 if (nacija[i] == 0) {
  46.                     c.signalAll();
  47.                 }
  48.             } finally { lock.unlock(); }
  49.         }
  50.     }
  51.    
  52.     private class Englez extends UtilThread {
  53.         public Englez(int id) {
  54.             super("\u0415\u043d\u0433\u043b\u0435\u0437 " + id, BOJA_ENGLEZ, odmaraEnglez);
  55.         }
  56.  
  57.         @Override
  58.         protected void step() {
  59.             odmara();
  60.             m.ulaz(0);
  61.             obilazi();
  62.             m.izlaz(0);
  63.         }
  64.     }
  65.  
  66.     private class Nemac extends UtilThread {
  67.         public Nemac(int id) {
  68.             super("\u041d\u0435\u043c\u0430\u0446 " + id, BOJA_NEMAC, odmaraNemac);
  69.         }
  70.  
  71.         @Override
  72.         protected void step() {
  73.             odmara();
  74.             m.ulaz(1);
  75.             obilazi();
  76.             m.izlaz(1);
  77.         }
  78.     }
  79.  
  80.     private class Italijan extends UtilThread {
  81.         public Italijan(int id) {
  82.             super("\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d " + id, BOJA_ITALIJAN, odmaraItalijan);
  83.         }
  84.  
  85.         @Override
  86.         protected void step() {
  87.             odmara();
  88.             m.ulaz(2);
  89.             obilazi();
  90.             m.izlaz(2);
  91.         }
  92.     }
  93.  
  94.     // Parametri simulacije
  95.     public static final int    DUZINA_OBILASKA   = 3000;
  96.     public static final int    DUZINA_ODMARANJA  = 5000;
  97.     public static final int    BROJ_DJAKA        = 8;
  98.  
  99.     // Boje
  100.     public static final Color  BOJA_ENGLEZ        = new Color(0xFF, 0x44, 0x44);
  101.     public static final Color  BOJA_NEMAC         = new Color(0x22, 0x22, 0x22);
  102.     public static final Color  BOJA_ITALIJAN      = new Color(0x44, 0xCC, 0x44);
  103.    
  104.     public static final Color  BOJA_POZ_ENGLEZI   = new Color(0xFF, 0x44, 0x44);
  105.     public static final Color  BOJA_POZ_NEMCI     = new Color(0x44, 0x44, 0x44);
  106.     public static final Color  BOJA_POZ_ITALIJANI = new Color(0x44, 0xCC, 0x44);
  107.     public static final Color  BOJA_MUZEJA        = null;
  108.  
  109.     // Stringovi
  110.     public static final String TEXT_OBILAZI         = "\u041e\u0431\u0438\u043b\u0430\u0437\u0438";
  111.     public static final String TEXT_OBISAO          = "\u041e\u0431\u0438\u0448\u0430\u043e";
  112.     public static final String TEXT_ODMARA          = "\u041E\u0434\u043C\u0430\u0440\u0430";
  113.     public static final String TEXT_ODMORIO         = "\u0421\u043F\u0440\u0435\u043C\u0430\u043D";
  114.     public static final String TEXT_GRUPA_ENGLEZI   = "\u0413\u0440\u0443\u043f\u0430\u0020\u0415\u043d\u0433\u043b\u0435\u0437\u0430";
  115.     public static final String TEXT_GRUPA_NEMCI     = "\u0413\u0440\u0443\u043f\u0430\u0020\u041d\u0435\u043c\u0430\u0446\u0430";
  116.     public static final String TEXT_GRUPA_ITALIJANI = "\u0413\u0440\u0443\u043f\u0430\u0020\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d\u0430";
  117.     public static final String TEXT_MUZEJ           = "\u041c\u0443\u0437\u0435\u0458 \"Louvre\"";
  118.  
  119.     private class UtilThread extends SimulationThread {
  120.  
  121.         private SimulationContainer odmara;
  122.  
  123.         protected UtilThread(String name, Color color, SimulationContainer klupa) {
  124.             super(name, color);
  125.             odmara = klupa;
  126.         }
  127.  
  128.         protected void odmara() {
  129.             setContainer(odmara);
  130.             setText(TEXT_ODMARA);
  131.             work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
  132.             setText(TEXT_ODMORIO);
  133.         }
  134.  
  135.         protected void obilazi() {
  136.             setContainer(obilazi);
  137.             setText(TEXT_OBILAZI);
  138.             work(DUZINA_OBILASKA, 2 * DUZINA_OBILASKA);
  139.             setText(TEXT_OBISAO);
  140.         }
  141.     }
  142.  
  143.     // Glavni program
  144.     public static void main(String[] a) {
  145.         new LouvreLock();
  146.     }
  147.  
  148.     // Stanja
  149.     private SimulationContainer obilazi = new SimulationContainer(TEXT_MUZEJ, BOJA_MUZEJA, SimulationContainerLayout.BOX);
  150.     private SimulationContainer odmaraEnglez = new SimulationContainer(TEXT_GRUPA_ENGLEZI, BOJA_POZ_ENGLEZI, SimulationContainerLayout.BOX);
  151.     private SimulationContainer odmaraNemac = new SimulationContainer(TEXT_GRUPA_NEMCI, BOJA_POZ_NEMCI, SimulationContainerLayout.BOX);
  152.     private SimulationContainer odmaraItalijan = new SimulationContainer(TEXT_GRUPA_ITALIJANI, BOJA_POZ_ITALIJANI, SimulationContainerLayout.BOX);
  153.     private SimulationContainer stajaliste = new SimulationContainer(SimulationContainerLayout.ROW, odmaraNemac, odmaraItalijan, odmaraEnglez);
  154.     private SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, stajaliste, obilazi);
  155.  
  156.     public LouvreLock() {
  157.         // Create frame
  158.         SimulationPanel panel = new SwingSimulationPanel(sve);
  159.         SimulationFrame frame = SimulationFrame.create("Louvre", panel, new NoAnimationPanel());
  160.         frame.display();
  161.  
  162.         // Create threads
  163.         for (int i = 1; i <= BROJ_DJAKA; i++) {
  164.             new Englez(i).start();
  165.             new Nemac(i).start();
  166.             new Italijan(i).start();
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement