Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. package vaterpolisti;
  2.  
  3. import java.awt.Color;
  4. import java.util.concurrent.Semaphore;
  5. //import os.SimulationThread;
  6. import os.simulation.SimulationThread;
  7. import os.simulation.containers.*;
  8. //import os.simulation.display.*;
  9. import os.simulation.frame.*;
  10.  
  11. public class VaterpoloSem {
  12.    
  13.     //Object lock = new Object();
  14.     Semaphore  crveni = new Semaphore(0);
  15.     Semaphore plavi = new Semaphore(0);
  16.     Semaphore beli = new Semaphore(0);
  17.     Semaphore mutex = new Semaphore (1);
  18.     int hoceCrveni, hocePlavi, hoceBeli = 0;
  19.    
  20.     private class Crveni extends UtilThread
  21.     {
  22.         public Crveni(int id)
  23.         {
  24.             super("\u0426\u0440\u0432\u0435\u043D\u0438 " + id, BOJA_CRVENI, odmaraCrveni);        
  25.         }
  26.        
  27.         @Override
  28.         protected void step()
  29.         {
  30.             odmara();
  31.             mutex.acquireUninterruptibly();
  32.             while((hocePlavi > 0) || (hoceBeli > 0))
  33.             {
  34.                 mutex.release();
  35.                 crveni.acquireUninterruptibly();
  36.                 mutex.acquireUninterruptibly();
  37.             }
  38.             ++hoceCrveni;
  39.             mutex.release();
  40.             trenira();
  41.             mutex.acquireUninterruptibly();
  42.             if (--hoceCrveni == 0)
  43.             {
  44.                 beli.release(beli.getQueueLength());
  45.                 plavi.release(plavi.getQueueLength());
  46.             }
  47.             mutex.release();
  48.         }
  49.     }
  50.    
  51.     private class Beli extends UtilThread
  52.     {
  53.         public Beli(int id)
  54.         {
  55.             super("\u0411\u0435\u043B\u0438 " + id, BOJA_BELI, odmaraBeli);        
  56.         }
  57.        
  58.         @Override
  59.         protected void step()
  60.         {
  61.             odmara();
  62.             mutex.acquireUninterruptibly();
  63.             while((hoceBeli > 0) || (hocePlavi > 0) || (hoceCrveni > 0))
  64.             {
  65.                 mutex.release();
  66.                 beli.acquireUninterruptibly();
  67.                 mutex.acquireUninterruptibly();
  68.             }
  69.             ++hoceBeli;
  70.             mutex.release();
  71.             trenira();
  72.             mutex.acquireUninterruptibly();
  73.             if (--hoceBeli == 0)
  74.             {
  75.                 plavi.release(plavi.getQueueLength());
  76.                 beli.release(beli.getQueueLength());
  77.                 crveni.release(crveni.getQueueLength());
  78.             }
  79.             mutex.release();
  80.         }
  81.     }
  82.    
  83.     private class Plavi extends UtilThread
  84.     {
  85.         public Plavi(int id)
  86.         {
  87.             super("\u041F\u043B\u0430\u0432\u0438 " + id, BOJA_PLAVI, odmaraPlavi);        
  88.         }
  89.        
  90.         @Override
  91.         protected void step()
  92.         {
  93.             odmara();
  94.             mutex.acquireUninterruptibly();
  95.             while((hoceBeli > 0) || (hoceCrveni > 0))
  96.             {
  97.                 mutex.release();
  98.                 plavi.acquireUninterruptibly();
  99.                 mutex.acquireUninterruptibly();
  100.             }
  101.             ++hocePlavi;
  102.             mutex.release();
  103.             trenira();
  104.             mutex.acquireUninterruptibly();
  105.             if (--hocePlavi == 0)
  106.             {
  107.                 crveni.release(crveni.getQueueLength());
  108.                 beli.release(beli.getQueueLength());
  109.             }
  110.             mutex.release();
  111.         }
  112.     }
  113.    
  114.    
  115.    
  116.     // Parametri simulacije
  117.     public static final int DUZINA_TRENIRANJA = 3000;
  118.     public static final int DUZINA_ODMARANJA  = 5000;
  119.     public static final int BROJ_IGRACA       = 7;
  120.    
  121.     // Boje
  122.     public static final Color  BOJA_PLAVI        = new Color(192, 192, 255);
  123.     public static final Color  BOJA_CRVENI       = new Color(255, 192, 192);
  124.     public static final Color  BOJA_BELI         = new Color(255, 255, 255);
  125.     public static final Color  BOJA_KLUPE_PLAVI  = new Color(0, 0, 128);
  126.     public static final Color  BOJA_KLUPE_CRVENI = new Color(128, 0, 0);
  127.     public static final Color  BOJA_KLUPE_BELI   = new Color(255, 255, 255);
  128.     public static final Color  BOJA_TERENA       = null;
  129.    
  130.     // Stringovi
  131.     public static final String TEXT_TRENIRA      = "\u0422\u0440\u0435\u043D\u0438\u0440\u0430";
  132.     public static final String TEXT_TRENIRAO     = "\u0417\u0430\u0432\u0440\u0448\u0438\u043E";
  133.     public static final String TEXT_ODMARA       = "\u041E\u0434\u043C\u0430\u0440\u0430";
  134.     public static final String TEXT_ODMORIO      = "\u0421\u043F\u0440\u0435\u043C\u0430\u043D";
  135.     public static final String TEXT_KLUPA_PLAVI  = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
  136.     public static final String TEXT_KLUPA_CRVENI = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
  137.     public static final String TEXT_KLUPA_BELI   = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
  138.     public static final String TEXT_BAZEN        = "\u0411\u0430\u0437\u0435\u043D";
  139.    
  140.    
  141.    
  142.    
  143.     public class UtilThread extends SimulationThread
  144.     {
  145.         private ThreadContainer odmara;
  146.        
  147.         protected UtilThread(String name, Color color, ThreadContainer klupa)
  148.         {
  149.             super(name, color);
  150.             odmara = klupa;
  151.         }
  152.        
  153.         @Override
  154.         protected void run()
  155.         {
  156.             while (!isStopRequested())
  157.             {
  158.                 step();
  159.             }
  160.         }
  161.        
  162.         protected void odmara()
  163.         {
  164.             setContainer(odmara);
  165.             setText(TEXT_ODMARA);
  166.             work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
  167.             setText(TEXT_ODMORIO);
  168.         }
  169.        
  170.         protected void trenira()
  171.         {
  172.             setContainer(trenira);
  173.             setText(TEXT_TRENIRA);
  174.             work(DUZINA_TRENIRANJA, 2 * DUZINA_TRENIRANJA);
  175.             setText(TEXT_TRENIRAO);
  176.         }
  177.     }
  178.    
  179.     // Glavni program
  180.    
  181.     public static void main(String [] a)
  182.     {
  183.         new VaterpoloSem();
  184.     }
  185.    
  186.     // Stanja
  187.     private ThreadContainer odmaraPlavi;
  188.     private ThreadContainer odmaraCrveni;
  189.     private ThreadContainer odmaraBeli;
  190.     private ThreadContainer trenira;
  191.    
  192.     public VaterpoloSem()
  193.     {
  194.         // Create states
  195.         trenira = new ThreadContainerBox(TEXT_BAZEN, BOJA_TERENA);
  196.         odmaraPlavi = new ThreadContainerBox(TEXT_KLUPA_PLAVI, BOJA_KLUPE_PLAVI);
  197.         odmaraCrveni = new ThreadContainerBox(TEXT_KLUPA_CRVENI, BOJA_KLUPE_CRVENI);
  198.         odmaraBeli = new ThreadContainerBox(TEXT_KLUPA_BELI, BOJA_KLUPE_BELI);
  199.        
  200.         // Create frame
  201.         StateRow klupe = new StateRow(odmaraCrveni, odmaraBeli, odmaraPlavi);
  202.         SimulationFrame frame = SimulationFrame.create("Timovi", new StateColumn(klupe, trenira));
  203.         //SimulationFrame frame2 = SimulationFrame.create
  204.         frame.display();
  205.        
  206.         // Create threads
  207.         for (int i = 1; i <= BROJ_IGRACA; i++)
  208.         {
  209.             new Plavi(i).start();
  210.             new Crveni(i).start();
  211.             new Beli(i).start();
  212.            
  213.         }
  214.        
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement